capypad
0 day streak
typescript / beginner
Snippet

Defining Object Shapes with Interfaces

Interfaces are a powerful way to define the 'shape' of an object, ensuring that it contains specific properties with correct types.

snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
interface Book {
title: string;
author: string;
pages: number;
}
 
const myBook: Book = {
title: "The TypeScript Way",
author: "John Smith",
pages: 250
};
Breakdown
1
interface Book { ... }
Declares a new interface named Book that acts as a blueprint for objects.
2
const myBook: Book = { ... }
Creates a constant that must adhere to the Book interface structure.