-
Notifications
You must be signed in to change notification settings - Fork 0
never
Jorge Castro edited this page Nov 10, 2025
·
1 revision
Let's imagine we want to get the area of circles and squares.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; sideLength: number };
type Shape = Circle | Square;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}If someone would like to add triangles...
type Triangle = { kind: "triangle"; base: number; height: number };
type Shape = Circle | Square | Triangle;The line const _exhaustiveCheck: never = shape; will show an error, saying that it should not happen. Very useful for exhaustive checking.
- Sync-Obsidian-with-GitHub-on-iOS
- Obsidian-community-plugins
- Optimizing-Novel-Word-Count-Plugin-for-Git-Users
- What is TypeScript, and why you should use it
- Quick setup
- Inference and type annotations
- Fundamental Types
- Object Types
- Union Types
- Type Aliases and Interfaces
- Type Assertions
- Literal Types
- null and undefined
- Narrowing
- Creating Types from Types
- Common Built‐in and Utility Types
- tsc CLI ‐ (The Compiler)
- Avoid using Object or {}
- Ignoring TypeScript errors
- TypeScript resources