UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

45 lines 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.narrowType = void 0; const EnumType_js_1 = require("../Type/EnumType.js"); const NeverType_js_1 = require("../Type/NeverType.js"); const UnionType_js_1 = require("../Type/UnionType.js"); const derefType_js_1 = require("./derefType.js"); function narrowType(type, predicate) { const derefed = (0, derefType_js_1.derefType)(type); if (derefed instanceof UnionType_js_1.UnionType || derefed instanceof EnumType_js_1.EnumType) { let changed = false; const types = []; for (const sub of derefed.getTypes()) { const derefedSub = (0, derefType_js_1.derefType)(sub); const narrowed = narrowType(derefedSub, predicate); if (!(narrowed instanceof NeverType_js_1.NeverType)) { if (narrowed === derefedSub) { types.push(sub); } else { types.push(narrowed); changed = true; } } else { changed = true; } } if (changed) { if (types.length === 0) { return new NeverType_js_1.NeverType(); } else if (types.length === 1) { return types[0]; } else { return new UnionType_js_1.UnionType(types); } } return type; } return predicate(derefed) ? type : new NeverType_js_1.NeverType(); } exports.narrowType = narrowType; //# sourceMappingURL=narrowType.js.map