Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
How to enforce generic types typescript function

I have the following function signature:

const renderMessage = <T extends A | B > 
(
  a: T,
  b: Map<T, Array<string>>, 
  c: T,
) => {

My problem is that I can call this function as follows:

type A = "A" | "B"
type B = "Z" | "Y"
let myA: A = "A"
let myB: B = "Z"
let myAMap: Map<A,Array<string>> = new Map(...)
let myBMap: Map<B,Array<string>> = new Map(...)
renderMessage(myA, myBMap, myA) // I want error here
renderMessage(myB, myAMap, my B) // I want error here

The reason for being able to do this is explained here. If I understood correctly, the structure of my Map object does not change based on T being A or B, so typescript won't complain. However, I am struggling to figure out how to enforce this.

EDIT

They are types and not classes. See Playground Link

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The T type is too ambiguous for the TypeScript inference. It resolves to the most narrow type that satisfies the whole signature: A | B.

I guess you just have to provide it yourself between angle brackets:

TypeScript playground

about 4 years ago · Juan Pablo Isaza Denunciar

0

The reason to have different classes - A and B - should be if one type of instance should hold differently structured data from the other, or if one type of instance should have different methods. If the data is structured the same way, and the methods are all the same as well, then there shouldn't be separate classes.

If you distinguish A and B in any way, the error will arise as desired:

class A{
  x = 'x'
}
class B{
  y = 'g'
}
const renderMessage = <T extends A | B > 
(
  a: T,
  b: Map<T, Array<string>>, 
  c: T,
) => {
}

let myA: A = new A()
let myB: B = new B()
let myAMap: Map<A,Array<string>> = new Map(...)
let myBMap: Map<B,Array<string>> = new Map(...)
renderMessage(myA, myBMap, myA) // I want error here
renderMessage(myB, myAMap, myB) // I want error here
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda