feat: resolve dictionary types in schematics

pull/5209/head
Arman Ozak 5 years ago
parent 28b13d3af3
commit 53defa8c72

@ -15,13 +15,18 @@ export function createTypeSimplifier() {
);
return type.split('.').pop()!;
});
return (type: string) => parseType(type).join(' | ');
return (type: string) => {
const parsed = parseType(type);
const last = parsed.pop()!;
return parsed.reduceRight((record, tKey) => `Record<${tKey}, ${record}>`, last);
};
}
export function createTypeParser(replacerFn = (t: string) => t) {
const normalizeType = createTypeNormalizer(replacerFn);
return (originalType: string) => flattenUnionTypes([], originalType).map(normalizeType);
return (originalType: string) => flattenDictionaryTypes([], originalType).map(normalizeType);
}
export function createTypeNormalizer(replacerFn = (t: string) => t) {
@ -32,14 +37,10 @@ export function createTypeNormalizer(replacerFn = (t: string) => t) {
};
}
export function flattenUnionTypes(types: string[], type: string) {
export function flattenDictionaryTypes(types: string[], type: string) {
type
.replace(/^{/, '')
.replace(/}$/, '')
.replace(/{/, '(')
.replace(/}/, ')')
.replace(/[}{]/g, '')
.split(':')
.filter(Boolean)
.forEach(t => types.push(t));
return types;

Loading…
Cancel
Save