typescript optional parameter type check -
i've got function (func) on class (myclass) optional parameter. type of optional parameter (myinterface) has optional properties.
i expected compiler error when call foo primitive number. thats not case. why that? , there way tell type system mark error?
interface myinterface { foo?: string } class myclass { func(b?: myinterface) : void {} } let c = new myclass(); c.func(); c.func({ foo: 'bar' }); c.func({ foo: 30 }); // compiler error: ok c.func({}); c.func(60); // no compiler error: not expect
the reason happens number
compatible {}
. (for example, imagine argument of type {tofixed: (n: number) => string}
, too, compatible number
).
you can think way: you can number, {foo?: string}
.
Comments
Post a Comment