How to Create an Object in TypeScript
Use objects in TypeScript when related values should stay grouped under named properties. Objects are perfect for users, API payloads, configs, component props, and reusable data models.
What you’ll build or solve
You’ll learn how to create objects in TypeScript with inline types, type aliases, and inferred shapes. You’ll also know when interfaces are a better long-term choice.
Learn TypeScript on Mimo
When this approach works best
This approach is the right choice when named fields describe one logical entity.
Common real-world scenarios include:
- User profiles
- API request bodies
- App configuration
- Product data
- Function options
This is a bad idea when the shape is reused widely and should become a shared interface or type alias.
Prerequisites
You only need:
- Basic TypeScript types
- Familiarity with properties and values
- Understanding of type annotations
Step-by-step instructions
Step 1: Create an object with an inline type
The most direct pattern is an inline object type.
const user: { name: string; age: number } = {
name: "Alex",
age: 28
};
This ensures the object shape stays typed.
Step 2: Use a reusable type alias
For repeated shapes, extract the type.
type User = {
name: string;
age: number;
};
const user: User = {
name: "Alex",
age: 28
};
This keeps larger codebases cleaner.
Step 3: Let TypeScript infer the object type
When the object is local, inference is often enough.
const settings = {
theme: "dark",
showSidebar: true
};
TypeScript infers the exact property types.
What to look for:
- Inline types work for local objects
- Type aliases improve reuse
- Inference is often enough
- Great for configs and payloads
- Use interfaces for shared contracts
Examples you can copy
Product
const product = {
name: "Laptop",
price: 999
};
API payload
type LoginPayload = {
email: string;
password: string;
};
Config
const config: { retries: number } = {
retries: 3
};
Common mistakes and how to fix them
Mistake 1: Missing required properties
What the reader might do:
const user: User = {
name: "Alex"
};
Why it breaks: required fields are missing.
Corrected approach:
Provide every required property.
Mistake 2: Using object as the type
What the reader might do:
const user: object = { name: "Alex" };
Why it breaks: property access becomes weakly typed.
Corrected approach:
Use an explicit shape.
Mistake 3: Repeating inline shapes everywhere
What the reader might do:
Rewrite { name: string; age: number } across files.
Why it breaks: maintenance becomes harder.
Corrected approach:
Extract a shared type alias or interface.
Troubleshooting
If property access errors, avoid the generic object type.
If shapes repeat, extract a reusable type.
If optional values exist, mark them with ?.
If the object only models contracts, prefer interfaces.
Quick recap
- Use inline object types for local shapes
- Extract shared type aliases for reuse
- Inference works well for local objects
- Avoid the generic
objecttype - Prefer interfaces for shared contracts
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot