Ad space (banner)
Lesson 7 / 20

Interfaces (interface)

This lesson covers interface, used to define the shape of an object. It's for anyone searching "TypeScript interface usage."

interface is a mechanism for defining "what properties this object should have." Try to create an object that doesn't match the defined shape, and TypeScript flags it as an error.

The example defines interface User { name: string; age: number; } and creates a user object matching that shape. Missing even one property produces an error.

A common early mistake is confusing interface with type (a type alias), covered later. Both can define an object's shape, but interface has a distinguishing feature: it can be re-declared under the same name and merged (declaration merging).

In real projects, defining the shape of data received from an API as an interface means "what fields does this data have" becomes understandable from the code alone.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)