TypeScript has a neat shortcut for defining and assigning properties. Keep reading to find out more!
In C# when defining a class with a property and a constructor, I could do something like this:
class Car { public string make; public Car( string manufacturer ) { make = manufacturer; } }
In TypeScript I can do exactly the same:
class Car { make: string; constructor( manufacturer: string ) { this.make = manufacturer; } }
But I can go one step further and define the property directly from the constructor:
class Car { constructor( public make: string ) { } }
Note several things:
There is no property defined in the class itself.
The constructor has a public modifier preceding the parameter.
There is no explicit assignment in the constructor.
This is referred to as parameter properties and saves on some code, perhaps useful for simple objects.