Q38: What are Constructor Parameter Properties?
Parameter properties allow declaring and assigning class member fields directly within constructor signatures by prefixing arguments with an access modifier or readonly.
Parameter Property Shorthand
// Shorthand syntax
class User {
constructor(public name: string, private secretId: number) {}
}
// Equivalent standard syntax:
// class User {
// public name: string;
// private secretId: number;
// constructor(name: string, secretId: number) {
// this.name = name;
// this.secretId = secretId;
// }
// }💡 Prefixing constructor parameters with public/private automatically creates and initializes matching fields.
Key Takeaways
- Reduces class property initialization boilerplate.