How to Use Constructors in TypeScript
Use constructors in TypeScript when a class should initialize required state the moment an instance is created. Constructors are perfect for setting default values, enforcing required inputs, and preparing reusable object instances.
What you’ll build or solve
You’ll learn how to use constructors in TypeScript with typed parameters, default values, and shorthand property initialization. You’ll also know how constructors improve class reliability.
Learn TypeScript on Mimo
When this approach works best
This approach is the right choice when every class instance needs a valid starting state.
Common real-world scenarios include:
- User models
- Product objects
- API clients
- UI controllers
- Service classes
This is a bad idea when the class has no state and only contains static utility methods.
Prerequisites
You only need:
- Basic TypeScript classes
- Familiarity with
this - Understanding of type annotations
Step-by-step instructions
Step 1: Add a constructor with typed parameters
A constructor runs when new creates an instance.
class User {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
Create an instance like this:
const user = new User("Alex", 28);
This guarantees every instance starts with the required data.
Step 2: Use shorthand property initialization
TypeScript supports constructor parameter properties.
class Product {
constructor(
public name: string,
public price: number
) {}
}
This automatically creates and assigns the properties.
It is a clean pattern for model-style classes.
Step 3: Add default values when useful
Constructors can support optional defaults.
class Timer {
constructor(public duration: number = 60) {}
}
This makes instance creation more flexible.
const timer = new Timer();
What to look for:
- Constructors run during instance creation
- Type all parameters
- Use
thisfor manual assignment - Parameter properties reduce repetition
- Defaults improve flexibility
Examples you can copy
User model
constructor(public name: string, public email: string) {}
API client
constructor(private token: string) {}
Default timer
constructor(public seconds: number = 30) {}
Common mistakes and how to fix them
Mistake 1: Forgetting this
What the reader might do:
name = name;
Why it breaks: the class property stays uninitialized.
Corrected approach:
Use this.name.
Mistake 2: Missing parameter types
What the reader might do:
constructor(name, age)
Why it breaks: type safety is lost.
Corrected approach:
Type every parameter.
Mistake 3: Repeating property declarations unnecessarily
What the reader might do:
Declare fields and manually assign simple values every time.
Why it breaks: the class becomes verbose.
Corrected approach:
Use parameter properties.
Troubleshooting
If properties stay undefined, verify constructor assignments.
If the class feels repetitive, use shorthand parameter properties.
If defaults do not apply, make sure the argument is optional or omitted.
If the class has no instance state, use static helpers instead.
Quick recap
- Constructors initialize instance state
- Type all parameters
- Use
thisor parameter properties - Add defaults when useful
- Great for reliable object setup
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