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 Footype 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'); // okayonChange('expire', new Date(2018, 3, 14)); // okayonChange('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.
If anyone still looks for implementation of valueof
for any purposes, this is a one I came up with:
type valueof<T> = T[keyof T]
Usage:
type actions = { a: { type: 'Reset' data: number } b: { type: 'Apply' data: string }}type actionValues = valueof<actions>
Works as expected :) Returns an Union of all possible types
There is another way to extract the union type of the object:
const myObj = { a: 1, b: 'some_string' } as const; type values = typeof myObj[keyof typeof myObj];
Result: 1 | "some_string"