0

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.

1
  • have you tried writing: (description as Map<string, string>).get('test') Commented Jun 16, 2020 at 5:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.