From cd23d3a226dd76e07fa71f5f19d4fd1f65859833 Mon Sep 17 00:00:00 2001 From: sinclair Date: Mon, 31 Jul 2023 23:45:41 +0900 Subject: [PATCH] support number | string union on mapped type --- src/typescript/typescript-to-typebox.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/typescript/typescript-to-typebox.ts b/src/typescript/typescript-to-typebox.ts index f5af8b2..c180683 100644 --- a/src/typescript/typescript-to-typebox.ts +++ b/src/typescript/typescript-to-typebox.ts @@ -546,9 +546,11 @@ export namespace TypeScriptToTypeBox { set.add('TemplateLiteralParser') set.add('TemplateLiteralGenerator') set.add('TTemplateLiteral') + set.add('TPropertyKey') set.add('TypeGuard') set.add('TSchema') - set.add('TRecord') + set.add('TString') + set.add('TNumber') set.add('TUnion') set.add('TLiteral') } @@ -558,17 +560,22 @@ export namespace TypeScriptToTypeBox { function MappedSupport() { return useMapped ? [ - 'type MappedConstraint = TTemplateLiteral | TUnion[]> | TLiteral', + 'type MappedContraintKey = TNumber | TString | TLiteral', + 'type MappedConstraint = TTemplateLiteral | TUnion | MappedContraintKey', 'type MappedFunction = (C: C) => S', - 'function Mapped>(C: C, F: F): TRecord> {', + '// prettier-ignore', + 'function Mapped>(C: C, F: F) {', ' return (', ' TypeGuard.TTemplateLiteral(C) ? (() => {', ' const E = TemplateLiteralParser.ParseExact(C.pattern)', ' const K = TemplateLiteralFinite.Check(E) ? [...TemplateLiteralGenerator.Generate(E)] : []', ' return Type.Object(K.reduce((A, K) => ({ ...A, [K]: F(Type.Literal(K) as any)}), {}))', ' })() :', - ' TypeGuard.TUnion(C) ? Type.Object(C.anyOf.reduce((A, K) => ({ ...A, [K.const]: F(K as any)}), {}))', - ' : Type.Object({ [C.const]: F(C) })) as any', + ' TypeGuard.TUnion(C) ? Type.Object(C.anyOf.reduce((A, K) => ({ ...A, [K.const]: F(K as any)}), {})) : ', + ' TypeGuard.TString(C) ? Type.Record(C, F(C)) : ', + ' TypeGuard.TNumber(C) ? Type.Record(C, F(C)) : ', + ' Type.Object({ [C.const]: F(C) })', + ' )', '}', ].join('\n') : ''