# Parameter Properties

> Shorthand for class property assignment.

Subject: TypeScript | Lesson 43 of 54 | 2 min read
Interactive lesson: https://codewithanimation.in/typescript/parameter-properties
Previous: [Classes & Visibility](https://codewithanimation.in/typescript/classes-and-visibility.md) | Next: [Abstract Classes & Methods](https://codewithanimation.in/typescript/abstract-classes-methods.md)

## Constructor Shorthand

TypeScript offers a shorthand for declaring a class property and assigning a constructor argument to it in a single step.

By adding a visibility modifier (`public`, `private`, `protected`) or `readonly` to a constructor argument, TypeScript automatically creates the property and assigns it for you.

## Killing Boilerplate

In standard JavaScript or older object-oriented languages, you have to write the property name three times: once to declare it, once in the constructor arguments, and once to assign it (`this.x = x`).

Parameter properties reduce this to a single, clean declaration.

## Syntax

```typescript
// ❌ The old, verbose way
class PointVerbose {
  public x: number;
  public y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

// ✅ The modern, shorthand way
class PointShorthand {
  constructor(public x: number, public y: number) {}
}
```

## Common mistake: Hidden Fields

The main downside of parameter properties is that they hide the shape of the class.

If you look at the top of the class body, it appears completely empty. You have to read the constructor signature to realize the class has state.

## Tip: Readonly works too!

You don't have to use an access modifier. Just using `readonly` triggers the shorthand: `constructor(readonly id: string) {}`.

## Try it: Try it

Compare the verbose standard class declaration against the clean parameter property shorthand.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/parameter-properties

## Check yourself

### Question 1

Which of these constructor arguments will automatically create a class property?

- A. constructor(name: string)
- B. constructor(public name: string)
- C. constructor(let name: string)

Correct answer: B

You must prefix the argument with an access modifier (public, private, protected) or 'readonly' to trigger the parameter property shorthand.

- Why not A: Without a modifier, it's just a normal argument.
- Why not C: Invalid syntax.

## Remember this

- Prefix constructor args with `public`, `private`, `protected`, or `readonly`.
- This automatically declares and assigns the property (`this.prop = prop`).
- It removes boilerplate but can make the top of the class look deceivingly empty.
