Lint Rules

useValidTypeof (since v0.7.0)

This rule is recommended by Rome.

This rule verifies the result of typeof $expr unary expressions is being compared to valid values, either string literals containing valid type names or other typeof expressions

Examples

Invalid

typeof foo === "strnig"
correctness/useValidTypeof.js:1:16 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo === "strnig"
                  ^^^^^^^^
    2 │ 
  
   not a valid type name
  
typeof foo == "undefimed"
correctness/useValidTypeof.js:1:15 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo == "undefimed"
                 ^^^^^^^^^^^
    2 │ 
  
   not a valid type name
  
typeof bar != "nunber"
correctness/useValidTypeof.js:1:15 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof bar != "nunber"
                 ^^^^^^^^
    2 │ 
  
   not a valid type name
  
typeof bar !== "fucntion"
correctness/useValidTypeof.js:1:16 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof bar !== "fucntion"
                  ^^^^^^^^^^
    2 │ 
  
   not a valid type name
  
typeof foo === undefined
correctness/useValidTypeof.js:1:16 lint/correctness/useValidTypeof  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo === undefined
                  ^^^^^^^^^
    2 │ 
  
   not a string literal
  
   Suggested fix: Compare the result of `typeof` with a valid type name
  
    1 │ typeof·foo·===·"undefined"
                 +         +
typeof bar == Object
correctness/useValidTypeof.js:1:15 lint/correctness/useValidTypeof  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof bar == Object
                 ^^^^^^
    2 │ 
  
   not a string literal
  
   Suggested fix: Compare the result of `typeof` with a valid type name
  
    1  - typeof·bar·==·Object
      1+ typeof·bar·==·"object"
    2 2  
  
typeof foo === baz
correctness/useValidTypeof.js:1:16 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo === baz
                  ^^^
    2 │ 
  
   not a string literal
  
typeof foo == 5
correctness/useValidTypeof.js:1:15 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo == 5
                 ^
    2 │ 
  
   not a string literal
  
typeof foo == -5
correctness/useValidTypeof.js:1:15 lint/correctness/useValidTypeof ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Invalid `typeof` comparison value
  
  > 1 │ typeof foo == -5
                 ^^
    2 │ 
  
   not a string literal
  

Valid

typeof foo === "string"
typeof bar == "undefined"
typeof bar === typeof qux