0
TypeScript · Basic Types
New technique
Primitives and arrays
Every forge sorts its iron. Let's sort our values by type.
You know string, number and boolean. An array of them gets [] after the type: number[] is a list of numbers, any length.
There's a second spelling that means exactly the same: the generic form Array<T>. Pick one style per project.
TypeScript infers arrays too. [1, 2, 3] becomes number[], and a mixed literal like ["axe", 3] becomes (string | number)[]: every element may be either.
let ids: number[] = [1, 2, 3]; let names: Array<string> = ["Brunn", "Hilde"]; let mixed: (string | number)[] = ["axe", 3]; let split: string[] | number[] = [1, 2]; // all strings OR all numbers
- Mind the brackets:
(string | number)[]allows mixing,string[] | number[]doesn't. - There's no separate integer or float type:
3and3.5are bothnumber.