Reputation: 1700
I have a class
class Clazz{constructor(public foo: string, public bar: string)}
and then I am creating an array like this:
var cls: Clazz[] = [{foo: 'Foo', bar: 'Bar'}, {foo:'Missing bar'}]
The type checker does not complain about the item with missing bar
. Is this by design? If yes, what is the motivation behind this behaviour?
Upvotes: 0
Views: 57
Reputation: 276239
The type checker does not complain about the item with missing bar
It does:
See the sample in the playground.
Note that your code also had a syntax error (which was also reported ... the constructor body was missing).
Upvotes: 2
Reputation: 6059
You can leave out arguments and they will be initialized with their default value. This usually is done if you don't know the desired value yet or you are fine with the default value being set. Additionally, if you have a class with a lot of properties, you might not want to set them all in the constructor call.
Upvotes: 0