Reputation: 179
I am using a variable which has 2 types either string or Map. I want to check whether the variable is in type of string or Map. For that I have used the following code.
// description is like this description: string | Map<string, string>
if (description instanceof String) {
return description;
}else {
return description?.get('test');
}
But it gives me the following error.
Property 'get' does not exist on type 'string | Map'. Property 'get' does not exist on type 'string'
Please help me to solve the problem. Thanks.
Upvotes: 0
Views: 34
Reputation: 99609
instanceOf String
will not do the right thing, but typeof description === 'string'
will, or alternatively reverse it and use instanceof Map
. Not every string is a String
object. Most aren't.
Upvotes: 2