I have the following types:
Foo {
foobar: any
}
Bar {
fooBarBar: any;
}
A function defined as such:
this.api.submit(param: Foo | Bar)
usage:
this.api.submit(param.foobar) // does not exist on Bar
Error: Property 'foobar' does not exist on type 'Foo| Bar'.
Property 'foobar' does not exist on type 'Bar '
My assumption was typescript would figure based on the union it could be either of these models, so why does it complain in this instance?
A workaround is to use the bracket notation param['foobar'] and the error will disappear...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…