angularjs - Typescript user defined type guard stopped working when assigned to a service object -
i'm using typescript angular , defined type guard in service this:
type types = typea | typeb | typec; function istypea(data: types): data typea { return (<typea>data).a !== undefined; } function istypeb(data: types): data typeb { return (<typeb>data).b !== undefined; } function istypec(data: types): data typec { return (<typec>data).c !== undefined; } let svc = this; svc.istypea = istypea; svc.istypeb = istypeb; svc.istypec = istypec;
and in service function use this:
if (istypea(data)) { // use data.a } else if (istypeb(data)) { // use data.b } else if (istypec(data)) { // use data.c }
it works fine in service, once assign guards service's this
object , use them in controller, got following error:
error ts2339: property 'a' not exist on type 'types'.
the guards used this:
// svc injected controller service if (svc.istypea(data)) { // use data.a } else if (svc.istypeb(data)) { // use data.b } else if (svc.istypec(data)) { // use data.c }
if redefine guards in controller, works fine again, suggestion have done wrong here?
Comments
Post a Comment