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’?


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;

Answered By – satanTime

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More