1

I can check if my variable is a string by using the code below

if (typeof myVariable === "string") {

}

but now I need to check if my variable is any or not. I try using the code below

if (typeof myVariable === "any") {

}

but I have error

enter image description here

This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"any"' have no overlap.ts(2367) Invalid typeof comparison value.

so how to solve this?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • 1
    You do know `typeof` is both a runtime JavaScript *and* compile-time TypeScript operator, right (currently, your usage is mixing both)? And that there is no `any` data type in JavaScript? And that `any` is not so much a type, but more of a type-checker off-switch, telling the compiler "anything goes here"? – Oleg Valter is with Ukraine Mar 21 '21 at 11:26

0 Answers0