typescript / beginner
Snippet
Defining an Interface
An Interface defines the structure of an object, ensuring it has specific properties with specific types.
snippet.ts
1
2
3
4
5
6
7
8
9
interface User {id: number;name: string;}const newUser: User = {id: 1,name: "John Doe"};
Breakdown
1
interface User { ... }
Creates a blueprint for an object named 'User'.
2
id: number;
Specifies that a User object must have a numeric 'id'.
3
const newUser: User = { ... };
Creates an object that strictly follows the User interface structure.