In TypeScript, parameter properties allow you to define and initialize class properties directly within the constructor’s parameters. This helps to simplify the code by eliminating the need to explicitly declare properties inside the class body.

You can also combine it with the readonly modifier, which makes the property immutable after it has been initialized.

Example: Parameter Properties

class Octopus {
  readonly numberOfLegs: number = 8;  // Regular property
  constructor(readonly name: string) {}  // Parameter property: `name` is automatically assigned as a readonly property
}

Key Points:

  1. Parameter properties are defined directly in the constructor, like readonly name: string. This automatically creates a property and initializes it.
  2. You don’t need to manually declare the property in the class body; it’s done for you when you define it in the constructor.
  3. The readonly modifier makes the property immutable after initialization.