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

Validate `keyof` and `typeof` #111

Closed kungfooman closed 6 months ago

kungfooman commented 6 months ago

Example:

const DataTypeMap = Object.freeze({
  float32: Float32Array,
  float64: Float64Array,
});
/**
 * @typedef {keyof typeof DataTypeMap} DataType
 */
/**
 * @param {DataType} a - Test argument.
 * @returns {DataType} Same as input.
 */
function identity(a) {
  return a;
}
identity(1); // Bad call
identity('float32'); // Good call

Current validation simply triggers unchecked:

image