- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
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.
๐งช 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).
