Basic Property Type Syntax
You'll learn how to define the exact "shape" of an object by specifying each property's name and type.
The Core Idea
In TypeScript, object types are described by listing each property name followed by a colon and its type. This is how you tell TypeScript "this object must have a property called X, and it must be of type Y."
Think of it like filling out a form: each field has a label (the property name) and a requirement for what kind of value goes there (the type).
The Syntax Pattern
The basic pattern is simple:
let person: { name: string; age: number };
Here, person must be an object with:
- A
nameproperty that's astring - An
ageproperty that's anumber
Each property follows the format: propertyName: type, and properties are separated by semicolons (or commas—both work).
In Action
let car: { brand: string; year: number; electric: boolean };
car = { brand: "Tesla", year: 2023, electric: true }; // ✓ Valid
You can use this syntax anywhere you'd write a type: variable declarations, function parameters, return types, and more. The object type literal describes the contract—what properties must exist and what types they hold.
Why Semicolons?
Property separators can be semicolons or commas. Semicolons are more common in type declarations, while commas feel more like regular JavaScript objects. Both are valid; choose what your team prefers.
Key Takeaway: Object types use { propertyName: type } syntax to define the exact structure an object must have—each property gets a name, a colon, and a type annotation.