I am getting the below error in my vscode:
[ts] Property 'getPatientAllAddress' does not exist on type 'Object'.
Why does it check for getPatientAllAddress() method which is getting added dynamically?
This usually does not happen in Javascript.
get addresses() {
if (this.data && this.data.hasOwnProperty("getPatientAllAddress")) {
return this.data.getPatientAllAddress();
}
}
How to supress/ignore this warning.
Up to this point this.data has had type Object. The error occurs because you're trying to access a .getPatientAllAddress() property on a variable of type Object. Even though you've confirmed logically that it should have that property, the compiler isn't yet smart enough to understand it should make that property available on that variable's interface.
Solution 1
If you don't have the noImplicitAny flag turned on you should be able to get away with changing the last line to
return data['getPatientAllAddress']();
Solution 2
Set the type of this.data to :{getPatientAllAddress?:Function} (or in a better world create the interface corresponding to this.data which includes that function).
using either Function or the appropriate more specific type.
Solution 3
Define an interface for the full data
interface Bleh {
getPatientAllAddress : Function
}
and a type guard
function isBleh(x:any) : x is Bleh {
return x && x.hasOwnProperty("getPatientAllAddress");
}
and use as
if (isBleh(this.data)) {
this.data.getPatientAllAddress();
}
Solution 1 is the simplest, Solution 2 is the most correct (though you should really also define everything else in that interface too, not just this optional function), and Solution 3 is halfway me showing off language features and halfway talking about what you'll need to do in situations where 1 and 2 aren't viable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With