json-schema-typescript-generator
Version:
Generate typescript types from json schemas
26 lines • 931 B
JavaScript
const PRIMITIVE_TYPES = new Map();
PRIMITIVE_TYPES.set('null', 'null');
PRIMITIVE_TYPES.set('boolean', 'boolean');
PRIMITIVE_TYPES.set('integer', 'number');
PRIMITIVE_TYPES.set('number', 'number');
PRIMITIVE_TYPES.set('string', 'string');
export const basicTypeGenerator = (locatedSchema, _gatheredInfo, _inputInfo) => {
const schemaTypes = locatedSchema.schema.type;
if (!schemaTypes || schemaTypes.size === 0) {
return;
}
const tsTypesSet = new Set();
Array.from(PRIMITIVE_TYPES.entries())
.filter(([schemaType, _]) => schemaTypes.has(schemaType))
.map(([_, tsType]) => tsType)
.forEach((tsType) => tsTypesSet.add(tsType));
const tsTypes = Array.from(tsTypesSet);
if (tsTypes.length === 0) {
return;
}
if (tsTypes.length === 1) {
return tsTypes[0];
}
return `(${tsTypes.join(' | ')})`;
};
//# sourceMappingURL=basic-type-generator.js.map