From 336a2ef580f1bd646c58508b4afa3e1d7a7ead79 Mon Sep 17 00:00:00 2001 From: sinclair Date: Thu, 23 Nov 2023 03:43:38 +0900 Subject: [PATCH] Reimplement Index Types --- src/typebox.ts | 8 ++++++-- test/static/indexed.ts | 7 +++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/typebox.ts b/src/typebox.ts index 87a91b2cd..be8309fb2 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -462,12 +462,16 @@ export type TIndexArray = // prettier-ignore export type TIndexIntersect = T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [...TIndexKeyResolve, ...TIndexIntersect] + ? L extends TUnion + ? [TUnion>, ...TIndexIntersect] + : [...TIndexKeyResolve, ...TIndexIntersect] : [] // prettier-ignore export type TIndexUnionGather = T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [TIndexKeyResolve, ...TIndexUnionGather] + ? L extends TIntersect + ? [TIntersect>, ...TIndexUnionGather] + : [TIndexKeyResolve, ...TIndexUnionGather] : [] // prettier-ignore export type TIndexUnionCheck = diff --git a/test/static/indexed.ts b/test/static/indexed.ts index 16600b379..419ef477a 100644 --- a/test/static/indexed.ts +++ b/test/static/indexed.ts @@ -192,10 +192,9 @@ import { Type, Static } from '@sinclair/typebox' const I = Type.Intersect([Type.Union([A, B]), Type.Intersect([C, D])]) const R = Type.Index(I, ['x', 'y']) - // - // TUnion<[TIntersect<[TString, TNumber]>, TIntersect<[TLiteral<1>, TNumber]>]> - // TUnion<[TIntersect<[TString, TNumber, TString, TString]>, TIntersect<[TLiteral<1>, TNumber, TNumber]>]> + // TUnion<[TIntersect<[TUnion<[TString, TNumber]>, TIntersect<[TString, TIntersect<[TString, TIntersect<[]>]>, TIntersect<[]>]>]>, TIntersect<...>]> // TUnion<[TIntersect<[TUnion<[TString, TNumber]>, TIntersect<[TString, TString]>]>, TIntersect<[TUnion<[TLiteral<1>, TNumber]>, TNumber]>]> + // TUnion<[TIntersect<[TUnion<[TString, TNumber]>, TString, TString]>, TIntersect<[TUnion<[TLiteral<1>, TNumber]>, TNumber]>]> type O = Static Expect(R).ToStatic() } @@ -212,7 +211,7 @@ import { Type, Static } from '@sinclair/typebox' const C = Type.Object({ x: Type.Literal('C'), y: Type.Number() }) const D = Type.Object({ x: Type.Literal('D') }) const I = Type.Union([A, B, C, D]) - const R = Type.Index(I, Type.Union([Type.Literal('x')])) + const R = Type.Index(I, ['x']) type O = Static Expect(R).ToStatic<'A' | 'B' | 'C' | 'D'>() }