Advertisement
Intermediate TypeScript, Classes: Access Modifiers, Abstract Classes & Implements

What is parameter property shorthand in a constructor, and what does it expand to?

Short answer

Prefixing a constructor parameter with an access modifier (public, private, protected, or readonly) automatically declares a class property of that name AND assigns the parameter's value to it, removing the need to write both a field declaration and an explicit `this.x = x`…

Save TypeScript mock interview
Advertisement
AModel answer
Prefixing a constructor parameter with an access modifier (public, private, protected, or readonly) automatically declares a class property of that name AND assigns the parameter's value to it, removing the need to write both a field declaration and an explicit `this.x = x` assignment.
class Point {
  constructor(private x: number, private y: number) {}
  // equivalent to declaring 'private x: number;' and 'this.x = x;' manually
}
Real-world example
Reducing constructor boilerplate in small value classes like Point, Money, or Coordinates.
Likely follow-ups
  • Can you mix parameter properties with regular constructor parameters in the same constructor?
Related topics