Parameter Properties

What

Parameter properties allow you to declare and initialize class properties directly within the constructor arguments, drastically reducing boilerplate code.

How

By adding an access modifier (public, private, protected, or readonly) to a constructor parameter, TypeScript automatically creates a property with that name and assigns the argument’s value to it:

// Traditional way
class Hero {
  name: string;
  constructor(name: string) { this.name = name; }
}

// Parameter property shorthand
class HeroShorthand {
  constructor(public name: string) {}
}

Why

This feature eliminates the redundant repetition of property names in the class body, constructor parameters, and constructor assignments. It results in much cleaner and more readable class definitions, especially for classes that primarily act as data containers.

Summary

Parameter properties are the “clean code” way to define simple class properties in TypeScript.