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 mapped types #118

Closed kungfooman closed 6 months ago

kungfooman commented 6 months ago

Fixes: #117

PR is adding this API to solve the problem:

const str = '{[Key in 1|2|3]: {testkey: [Key, Key, Key], bla: Key}}';
const mapping = expandType(str);
const type = createTypeFromMapping(mapping);
const keys = Object.values(type.properties).map(_ => _.properties.bla);
const ret = keys[0] === 1 && keys[1] === 2 && keys[2] === 3;
console.log(`The keys should be [1, 2, 3]: ${keys} (${ret})`, {keys, type});

Basically type is an intermediary type, which isn't a mapped type any longer... but expanded - which then can be used normally to compare a value with a type via validateType again.