Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the T[number] mean in typescript code?

found the code first in http://www.typescriptlang.org/docs/handbook/advanced-types.html#example-1

and then found this similar code in typescript codebase: https://github.com/microsoft/TypeScript/blob/master/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L75

type KnockoutObservable<T> = { object: T };
type KnockoutObservableArray<T> = { array: T };

type KnockedOut<T> = T extends any[] ? 
                       KnockoutObservableArray<T[number]> : 
                       KnockoutObservable<T>;

type KnockedOutObj<T> = {
    [P in keyof T]: KnockedOut<T[P]>;
}

what is the T[number] mean in the code?

in the Typescript Handbook example, it said

the element type of the array as T[number]

while test in the playground(just for testing), replace T[number] with T or T[any] seems no different, but can not replace with T[string](why?).

the [number] after T seems not an index.

like image 470
db9035 Avatar asked Dec 05 '19 03:12

db9035


People also ask

What does T in TypeScript mean?

'T' is going to be a type declared at run-time instead of compile time. The T variable could be any non-declared variable (I couldn't find a reference, but I would assume any valid set of characters that could be used for a variable names).

What is default value of number in TypeScript?

We didn't have to explicitly type the by parameter, because TypeScript can infer its type based on the default value. If the multiply function is invoked without a value for the by parameter, or is explicitly passed undefined , the argument will be replaced with the default value of 10 .

What does the IS keyword do in TypeScript?

The is keyword is actually casting the type and can catch type errors later in the code. See this example for more info. @benjaminz I don't see how it could be handled by a boolean. Typescript needs to know that the function into which you pass an object is functioning like a type guard.

How do you initialize a number in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.


1 Answers

Array is declared using an index signature, something like this:

interface ArrayMaybe<Element> {
    [index: number]: Element;
}

(I call this ArrayMaybe because I haven’t specifically copied Array’s declaration, and it’s not relevant.)

Index signatures indicate that a type can have properties using any value of that type as the key or index, but with the same type of value at each such key/index. So ArrayMaybe<number> can have values at 0, 1, 42, and so on, but wherever it has such properties, the property’s value will be a number.

Coming from the other side of things, when talking about some type T, T[____] references some particular property of T. So { foo: 'bar'; }['foo'] will refer to type 'bar'. In the case of an index signature as we see above, we can use T[number] to refer to the type of that index signature—in the case of ArrayMaybe, that is Element. Which is precisely how it’s being used in your example.

You can’t use T[string] because Array doesn’t have a string index signature. You’re allowed to use those, but Array doesn’t. Since it doesn’t have a string index signature, T[string] isn’t legal. You can use T['length'], though, since Array does have a property with that particularly string. Using string or number refers to any string or number—which requires an index signature.

For an example of a string index signature, consider

interface Dictionary<Value> {
    [key: string]: Value;
}

With this, we can use T[string] when T is some Dictionary—and T[string] will be Value.

like image 69
KRyan Avatar answered Oct 16 '22 13:10

KRyan