In TS, why number[][number] === number?
That's a way how you get union of types of values.
it is the same as: there is an array of numbers number[]
, we say [number]
, that means give us a union of types of values that belong to numeric key, because it's an array of numbers the result is number
.
type TestObject = {
test1: string;
test2: number;
};
// union of all keys is string | number
type TestValueUnion = TestObject[keyof TestObject];
// 'test1' | 'test2'
type TestKeyUnion = keyof TestObject;