Parameter Properties, Private or Protected Constructor Arguments
One neat feature that I only just learned about is the idea of “Parameter Properties”
You’ve probably written code like this before:
class Example {
private readonly name: string;
constructor(name: string) {
this.name = name;
}
}
But through the magic of parameter properties, you can write the same code like this:
class Example {
constructor(private readonly name: string) {}
}
Obviously there’s a benefit of less typing that comes from it, but it does come with a bit of a concern: Normally, it’s easy enough to scan the top of a class to find the list of attributes that will exist– so this does obscure it a bit.
Either way, I thought it was neat and look forward to using it from time to time, especially for simpler classes.