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

330
Vistas
In Typescript, can I constrain the type of the value at a generic key?

In Typescript, how can I constrain the type a field on a generic type to a particular type? In other words, I want to write a function that accepts an object obj and a key key of that object, but raises a Typescript error if obj[key] is not of type X. For instance, if X is string:

function foo<T extends {}, K extends keyof T ???>(obj: T, key: K) { 
  console.log(obj[key])
}

>> foo({ fox: 'Mulder' }, 'fox')
Mulder

>> foo({ fox: 22 }, 'fox')
<some error here>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Guerric's answer demonstrates how type parameters are usually constrained: 1) define T constrained to be an object, then 2) define K constrained to be a key of T.

For the OP's case, however, I would reverse this logic: 1) define Key constrained to be a generic property key (a.k.a string | number | symbol), then 2) define Obj constrained to be an object containing property Key:

function foo<Key extends PropertyKey, Obj extends Record<Key, string>>(obj: Obj, key: Key) {
    console.log(obj[key]);
}

foo({ fox: 'Mulder' }, 'fox'); // works fine

foo({ fox: 22 }, 'fox'); // Error (number is not assignable to string)

Try it.

This way, you get readable errors (in this context, "number is not assignable to string" is much less confusing than "string is not assignable to never"), and also the implementation of the function (the console.log(obj[key]) bit) does not require any typecasting since obj is properly typed to have a Key key.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do like this:

function foo<T extends {}, K extends keyof T>(obj: T, key: T[K] extends string ? T[K] : never) { 
  console.log((obj as any)[key]);
}

foo({ fox: 'Mulder' }, 'fox'); // OK


foo({ fox: 22 }, 'fox'); // Argument of type 'string' is not assignable to parameter of type 'never'

It implies to cast obj as any in the implementation because never can't be used as an index type, even though nothing is assignable to never so a function call with an actual key parameter that would make key type resolve to never will never compile (by definition), but TypeScript is not clever enough to infer that.

TypeScript playground

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