• AndyH

Parameter properties

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:

  1. There is no property defined in the class itself.

  2. The constructor has a public modifier preceding the parameter.

  3. 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.

10 views0 comments

Recent Posts

See All


© 2019 AndyH  -  all rights reserved