Menu

Vue js , TypeScript class object initial field values

Recently ran into some issues (Colleague of mine noticed it) working with VueJs (maybe its the same in Angular, but can’t remember).

The issue is if you have a class implementation as below

And if you initialise a new object like below

Properties won’t get default values, for example in C# if you do this, string fields will have an empty string (”), bool will have false, so on…

But it’s not the case with TypeScipt/Vue/Js (well, not sure – someone please enlighten me!!!!)

You either need to set the initial values  in the constructor() liek below

Or even better

Alternatively, you can do it like this too.

and you can init values like new Asset(0);

Sweet

 

Leave a comment