C.W.K.
Stream
Lesson 05 of 06 · published

Parameter Properties: Constructor Shorthand

~7 min · classes, parameter-properties, shorthand

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete
"Declare a field, assign it, and apply an access modifier — all in the parameter list."

The shorthand

TypeScript supports a special syntax for constructor parameters: prefix the parameter with an access modifier (public, private, protected, readonly), and TypeScript declares a class field with that name, automatically assigns it from the parameter, and applies the modifier.

class User {
  constructor(public name: string, private _email: string) {}
  // Equivalent to:
  // name: string;
  // private _email: string;
  // constructor(name: string, _email: string) {
  //   this.name = name;
  //   this._email = _email;
  // }
}

Three things in one parameter: field declaration, modifier, assignment. The shorthand keeps class definitions concise without losing any of the type system's checks.

When parameter properties shine

Data-bag classes — where the constructor just stores its parameters as fields — collapse dramatically. A class with 5 fields and a constructor that assigns each becomes 6 lines instead of 15. The shorthand is especially common in dependency-injection patterns (Angular, NestJS) where constructors are mostly assignments.

When to skip the shorthand

If the constructor does anything else with its parameters — validation, derivation, side effects — keep the explicit form. The shorthand only works for direct field assignment; once you need logic, you have to be explicit anyway.

Parameter properties trade explicitness for concision in the common case. Use them when the constructor's only job is to assign parameters to fields. Switch to explicit form the moment the constructor needs to do anything else.

Code

Parameter properties vs explicit form·typescript
// Parameter properties — three things in one.
class User {
  constructor(
    public id: number,
    public name: string,
    private _email: string,
    readonly createdAt: Date = new Date(),
  ) {}

  get email(): string {
    return this._email;
  }
}

const u = new User(1, 'Pippa', 'a@b.c');
u.name;            // ✅ public
u.id;              // ✅ public
u.createdAt;       // ✅ readonly
u.email;           // ✅ via getter
// u._email;       // ❌ private
// u.createdAt = new Date();  // ❌ readonly

// Explicit equivalent — much longer.
class UserExplicit {
  public id: number;
  public name: string;
  private _email: string;
  readonly createdAt: Date;

  constructor(id: number, name: string, _email: string, createdAt: Date = new Date()) {
    this.id = id;
    this.name = name;
    this._email = _email;
    this.createdAt = createdAt;
  }
}

External links

Exercise

Rewrite a class you've written using parameter properties. Compare the line count before and after. Then add validation logic to the constructor — what happens to the shorthand?
Hint
If the constructor needs to validate (e.g., throw on invalid input) before assigning, you can't use the shorthand for fields that depend on the validation result. The shorthand works for trivial assignment; logic forces the explicit form.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.