olafc
olafc

Reputation: 13

In TS, why number[][number] === number?

enter image description here

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

Answers (2)

cai zhang
cai zhang

Reputation: 1

example

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

satanTime
satanTime

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

Related Questions