Is there a `valueof` similar to `keyof` in TypeScript?

UPDATE: Looks like the question title attracts people looking for a union of all possible property value types, analogous to the way keyof gives you the union of all possible property key types. Let’s help those people first. You can make a ValueOf analogous to keyof, by using indexed access types with keyof T as the key, like so:

type ValueOf<T> = T[keyof T];

which gives you

type Foo = { a: string, b: number };
type ValueOfFoo = ValueOf<Foo>; // string | number

For the question as stated, you can use individual keys, narrower than keyof T, to extract just the value type you care about:

type sameAsString = Foo['a']; // look up a in Foo
type sameAsNumber = Foo['b']; // look up b in Foo

In order to make sure that the key/value pair “match up” properly in a function, you should use generics as well as indexed access types, like this:

declare function onChange<K extends keyof JWT>(key: K, value: JWT[K]): void; 
onChange('id', 'def456'); // okay
onChange('expire', new Date(2018, 3, 14)); // okay
onChange('expire', 1337); // error. 1337 not assignable to Date

The idea is that the key parameter allows the compiler to infer the generic K parameter. Then it requires that value matches JWT[K], the indexed access type you need.

Leave a Comment