capypad
0 day streak
typescript / beginner
Snippet

Union Types

Union types allow a variable to be one of several types. This is useful when a value could legitimately be different types.

snippet.ts
typescript
1
2
3
let result: string | number;
result = "Success";
result = 200;
Breakdown
1
string | number
The vertical bar (|) means 'or', so the variable can be a string OR a number.