Home > OS >  In Typescript, can I constrain the type of the value at a generic key?
In Typescript, can I constrain the type of the value at a generic key?

Time:12-02

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>

CodePudding user response:

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

CodePudding user response:

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.

  • Related