Primitive types in TypeScript
Annotating the basic types, arrays, and tuples.
const name: string = "typestar";
const count: number = 42;
const ready: boolean = true;
const tags: string[] = ["code", "typing"];
const pair: [string, number] = ["score", 95];
let maybe: string | undefined;
const values: readonly number[] = [1, 2, 3];
function describe(input: string | number): string {
return typeof input === "string" ? input.trim() : input.toFixed(1);
}
How it works
- Annotations follow the name after a colon.
[string, number]is a fixed-length tuple.- A union parameter is narrowed with
typeof.
Keywords and builtins used here
booleanconstfunctionletnumberreadonlyreturnstring
The run, in numbers
- Lines
- 12
- Characters to type
- 376
- Tokens
- 104
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 73 seconds.
Step 1 of 4 in Primitives & literals, step 1 of 23 in Types & annotations.