capypad
0 day streak
typescript / beginner
Snippet

Creating Reusable Type Aliases

Type aliases allow you to create a new name for a type. They are similar to interfaces but can also represent primitive types or complex structures.

snippet.ts
typescript
1
2
3
4
5
6
7
type Point = {
x: number;
y: number;
};
 
const origin: Point = { x: 0, y: 0 };
const target: Point = { x: 10, y: 20 };
Breakdown
1
type Point = { ... }
Creates a type alias named Point for an object with x and y coordinates.
2
const origin: Point
Uses the Point alias to ensure the object follows the predefined coordinate structure.