Reputation: 13
type Test = number[][number]; // Test will be inferred to 'number' as the above image show.
Why number[]
is an index signature type with signature 'number'?
Upvotes: 1
Views: 408
Reputation: 1
number[] mean a array
when you want to get the type of array, you can add [randomInput]
so number[][number] mean get the type of number[]
number[][xxx] is also equal number try it
Upvotes: 0
Reputation: 13574
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;
Upvotes: 2