kungfooman / RuntimeTypeInspector.js

Checking JSDoc types at runtime for high-quality types - Trust is good, control is better.
MIT License
8 stars 0 forks source link

Fix number unions #96

Closed kungfooman closed 7 months ago

kungfooman commented 7 months ago

Test:

/**
 * @param {1|2|3} a
 */
function square123(a, b) {
  return a * a;
}
square123( 0 ); // error
square123( 1 );
square123( 2 );
square123("2"); // error
square123( 3 );
square123( 4 ); // error
square123("4"); // error

Current output:

/**
 * @param {1|2|3} a
 */
function square123(a, b) {
  if (!assertType(a, {
    "type": "union",
    "members": [
      "1",
      "2",
      "3"
    ],
    "optional": false
  }, 'square123', 'a')) {
    youCanAddABreakpointHere();
  }
  return a * a;
}
square123(0); // error

square123(1);
square123(2);
square123("2"); // error

square123(3);
square123(4); // error

square123("4"); // error

DevTools:

image

There are multiple ways to fix this, but I have to contemplate that a bit more for the PR...