Interfaces and type aliases in TypeScript
Describing object shapes, and combining them.
interface User {
readonly id: number;
name: string;
email?: string;
}
interface Admin extends User {
permissions: string[];
}
type Point = { x: number; y: number };
type Labeled = Point & { label: string };
const admin: Admin = {
id: 1,
name: "ada",
permissions: ["read", "write"],
};
How it works
readonlyand?mark immutable and optional fields.extendsbuilds a wider interface.&intersects two types into one.
Keywords and builtins used here
Adminconstextendsinterfacenumberreadonlystringtype
The run, in numbers
- Lines
- 18
- Characters to type
- 287
- Tokens
- 77
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 49 seconds.
Step 1 of 5 in Objects & interfaces, step 11 of 23 in Types & annotations.