Grokking TypeScript
Vote

0% completed

​

Understanding Interfaces

Writing the same object type on every variable soon causes a problem. The shape { name: string; age: number } would appear at each declaration and in each function parameter, and one new property would mean editing all of those places. An interface solves this by giving an object type a name that you can reuse.

Defining an Interface

The interface keyword comes first, followed by the name, and then the properties the object must have.

interface User { name: string; age: number; } let user: User = { name: "Alice", age: 30 }; // Allowed

The compiler treats

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content