From 83e05bb43ca79cb22c9efc27e2e16caf7cb6a0e5 Mon Sep 17 00:00:00 2001 From: sinclairzx81 Date: Sat, 22 Jun 2024 16:20:08 +0900 Subject: [PATCH] Revision 0.32.34 (#914) * Fix Embedded TemplateLiteral Generation * TypeScript 5.5.2 * Version --- hammer.mjs | 2 +- package-lock.json | 18 ++++----- package.json | 4 +- src/type/template-literal/generate.ts | 1 + .../type/guard/type/template-literal.ts | 37 ++++++++++++++++++- test/static/template-literal.ts | 15 ++++++++ 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/hammer.mjs b/hammer.mjs index d936b2034..ef5d2b894 100644 --- a/hammer.mjs +++ b/hammer.mjs @@ -32,7 +32,7 @@ export async function benchmark() { // Test // ------------------------------------------------------------------------------- export async function test_typescript() { - for (const version of ['4.9.5', '5.0.4', '5.1.3', '5.1.6', '5.2.2', '5.3.2', '5.3.3', '5.4.3', 'next', 'latest']) { + for (const version of ['4.9.5', '5.0.4', '5.1.3', '5.1.6', '5.2.2', '5.3.2', '5.3.3', '5.4.3', '5.4.5', 'next', 'latest']) { await shell(`npm install typescript@${version} --no-save`) await test_static() } diff --git a/package-lock.json b/package-lock.json index 5fa9b2127..443474f51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.32.33", + "version": "0.32.34", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.32.33", + "version": "0.32.34", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", @@ -17,7 +17,7 @@ "ajv-formats": "^2.1.1", "mocha": "^10.4.0", "prettier": "^2.7.1", - "typescript": "^5.4.5" + "typescript": "^5.5.2" } }, "node_modules/@andrewbranch/untar.js": { @@ -1576,9 +1576,9 @@ } }, "node_modules/typescript": { - "version": "5.4.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", - "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "version": "5.5.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.2.tgz", + "integrity": "sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -2738,9 +2738,9 @@ "dev": true }, "typescript": { - "version": "5.4.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", - "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "version": "5.5.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.2.tgz", + "integrity": "sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==", "dev": true }, "undici-types": { diff --git a/package.json b/package.json index eeb755cc5..0afff5aa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typebox", - "version": "0.32.33", + "version": "0.32.34", "description": "Json Schema Type Builder with Static Type Resolution for TypeScript", "keywords": [ "typescript", @@ -37,6 +37,6 @@ "ajv-formats": "^2.1.1", "mocha": "^10.4.0", "prettier": "^2.7.1", - "typescript": "^5.4.5" + "typescript": "^5.5.2" } } diff --git a/src/type/template-literal/generate.ts b/src/type/template-literal/generate.ts index 9bc857fd7..22d87a99a 100644 --- a/src/type/template-literal/generate.ts +++ b/src/type/template-literal/generate.ts @@ -83,6 +83,7 @@ type TFromTemplateLiteralUnionKinds = type TFromTemplateLiteralKinds = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? ( + L extends TTemplateLiteral ? TFromTemplateLiteralKinds<[...S, ...R], Acc> : L extends TLiteral ? TFromTemplateLiteralKinds : L extends TUnion ? TFromTemplateLiteralKinds]> : L extends TBoolean ? TFromTemplateLiteralKinds : diff --git a/test/runtime/type/guard/type/template-literal.ts b/test/runtime/type/guard/type/template-literal.ts index 7b0f98ce4..63fb736aa 100644 --- a/test/runtime/type/guard/type/template-literal.ts +++ b/test/runtime/type/guard/type/template-literal.ts @@ -1,5 +1,5 @@ import { TypeGuard } from '@sinclair/typebox' -import { Type } from '@sinclair/typebox' +import { Type, TemplateLiteralGenerate } from '@sinclair/typebox' import { Assert } from '../../../assert/index' describe('guard/type/TTemplateLiteral', () => { @@ -44,4 +44,39 @@ describe('guard/type/TTemplateLiteral', () => { T.pattern = T.pattern.slice(0, T.pattern.length - 1) Assert.IsFalse(TypeGuard.IsTemplateLiteral(T)) }) + // ---------------------------------------------------------------- + // issue: https://github.com/sinclairzx81/typebox/issues/913 + // ---------------------------------------------------------------- + it('Should generate embedded template literal 1', () => { + const A = Type.TemplateLiteral([Type.Union([Type.Literal('A'), Type.Literal('B')])]) + const B = Type.TemplateLiteral([Type.Union([Type.Literal('X'), Type.Literal('Y')])]) + const L = Type.TemplateLiteral([Type.Literal('KEY'), A, B]) + const T = TemplateLiteralGenerate(L) + Assert.IsEqual(T, ['KEYAX', 'KEYAY', 'KEYBX', 'KEYBY']) + }) + it('Should generate embedded template literal 2', () => { + const A = Type.TemplateLiteral([Type.Union([Type.Literal('A'), Type.Literal('B')])]) + const B = Type.TemplateLiteral([Type.Union([Type.Literal('X'), Type.Literal('Y')])]) + const L = Type.TemplateLiteral([A, Type.Literal('KEY'), B]) + const T = TemplateLiteralGenerate(L) + Assert.IsEqual(T, ['AKEYX', 'AKEYY', 'BKEYX', 'BKEYY']) + }) + it('Should generate embedded template literal 3', () => { + const A = Type.TemplateLiteral([Type.Union([Type.Literal('A'), Type.Literal('B')])]) + const B = Type.TemplateLiteral([Type.Union([Type.Literal('X'), Type.Literal('Y')])]) + const L = Type.TemplateLiteral([A, B, Type.Literal('KEY')]) + const T = TemplateLiteralGenerate(L) + Assert.IsEqual(T, ['AXKEY', 'AYKEY', 'BXKEY', 'BYKEY']) + }) + it('Should map embedded template literal', () => { + const A = Type.TemplateLiteral([Type.Union([Type.Literal('A'), Type.Literal('B')])]) + const B = Type.TemplateLiteral([Type.Union([Type.Literal('X'), Type.Literal('Y')])]) + const L = Type.TemplateLiteral([Type.Literal('KEY'), A, B]) + const T = Type.Mapped(L, (K) => Type.Null()) + Assert.IsTrue(TypeGuard.IsObject(T)) + Assert.IsTrue(TypeGuard.IsNull(T.properties.KEYAX)) + Assert.IsTrue(TypeGuard.IsNull(T.properties.KEYAY)) + Assert.IsTrue(TypeGuard.IsNull(T.properties.KEYBX)) + Assert.IsTrue(TypeGuard.IsNull(T.properties.KEYBY)) + }) }) diff --git a/test/static/template-literal.ts b/test/static/template-literal.ts index 773ee1fa1..574d0e949 100644 --- a/test/static/template-literal.ts +++ b/test/static/template-literal.ts @@ -112,3 +112,18 @@ import { Type } from '@sinclair/typebox' '$propAx}Yx' | '$propBx}Yx' | '$propCx}Yx' >() } +// --------------------------------------------------------------------- +// issue: https://github.com/sinclairzx81/typebox/issues/913 +// --------------------------------------------------------------------- +{ + const A = Type.TemplateLiteral([Type.Union([Type.Literal('A'), Type.Literal('B')])]) + const B = Type.TemplateLiteral([Type.Union([Type.Literal('X'), Type.Literal('Y')])]) + const L = Type.TemplateLiteral([Type.Literal('KEY'), A, B]) + const T = Type.Mapped(L, (K) => Type.Null()) + Expect(T).ToStatic<{ + KEYAX: null + KEYAY: null + KEYBX: null + KEYBY: null + }>() +}