One-Line Constructor + Field Declaration
Parameter Properties
Add an access modifier on a constructor parameter to declare AND assign the field in one go.
What you'll learn
- Use parameter properties to shorten constructors
- Combine with `readonly` for immutable fields
Writing a class with a few fields is repetitive. Parameter properties collapse it.
The Long Way
class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
} Each field is declared, then assigned. Lots of repetition.
The Short Way
class Point {
constructor(public x: number, public y: number) {}
}
const p = new Point(3, 4);
p.x; // 3
p.y; // 4 Adding public (or private / protected / readonly) on a
constructor parameter tells TS:
- Declare a field with that name and modifier
- Assign the parameter to it on construction
One line — done.
With readonly
class User {
constructor(
public readonly id: string,
public name: string,
private email: string,
) {}
}
const u = new User("u1", "Ada", "ada@example.com");
u.id; // "u1"
u.id = "x"; // ✗ readonly
u.email; // ✗ private Mix and match modifiers. Each parameter property is independent.
When Not To
If the constructor needs to do anything beyond assignment — validation, transformation, derived state — a regular field + explicit assignment is clearer. Parameter properties are for the straight pass-through case.
Up Next
Decorators — annotations that modify classes and methods.
Decorators →