Search code examples
javascripttypescriptnot-operator

Is the not operator (!) behaving differently in TypeScript?


The following values are considered "falsy" in JavaScript:

  • false
  • 0
  • ''
  • ""
  • null
  • undefined
  • NaN

Does TypeScript handle the not operator (!) differently or is it the same?


Solution

  • One of the design goals of Typescript is:

    Preserve runtime behavior of all JavaScript code.

    This means that ALL Javascript code will behave the same in Typescript code. No exceptions. You might get extra semantic errors, but if you ignore those the behavior is still the same.

    So if you ever wonder if an operator such as ! behaves differently in TS, the answer will always be no