Skip to content

Commit

Permalink
Reimplement Index Types
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 22, 2023
1 parent 1e92672 commit 336a2ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,16 @@ export type TIndexArray<T extends TSchema, K extends PropertyKey> =
// prettier-ignore
export type TIndexIntersect<T extends TSchema[], K extends PropertyKey> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? [...TIndexKeyResolve<L, K>, ...TIndexIntersect<R, K>]
? L extends TUnion<infer U>
? [TUnion<TIndexUnion<U, K>>, ...TIndexIntersect<R, K>]
: [...TIndexKeyResolve<L, K>, ...TIndexIntersect<R, K>]
: []
// prettier-ignore
export type TIndexUnionGather<T extends TSchema[], K extends PropertyKey> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? [TIndexKeyResolve<L, K>, ...TIndexUnionGather<R, K>]
? L extends TIntersect<infer U>
? [TIntersect<TIndexIntersect<U, K>>, ...TIndexUnionGather<R, K>]
: [TIndexKeyResolve<L, K>, ...TIndexUnionGather<R, K>]
: []
// prettier-ignore
export type TIndexUnionCheck<T extends TSchema[][]> =
Expand Down
7 changes: 3 additions & 4 deletions test/static/indexed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof R>
Expect(R).ToStatic<number | string>()
}
Expand All @@ -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<typeof R>
Expect(R).ToStatic<'A' | 'B' | 'C' | 'D'>()

Check failure on line 216 in test/static/indexed.ts

View workflow job for this annotation

GitHub Actions / TypeBox (16.x, ubuntu-latest)

Property 'ToStatic' does not exist on type '{ ToStaticNever(): void; }'.

Check failure on line 216 in test/static/indexed.ts

View workflow job for this annotation

GitHub Actions / TypeBox (18.x, ubuntu-latest)

Property 'ToStatic' does not exist on type '{ ToStaticNever(): void; }'.

Check failure on line 216 in test/static/indexed.ts

View workflow job for this annotation

GitHub Actions / TypeBox (18.x, macOS-latest)

Property 'ToStatic' does not exist on type '{ ToStaticNever(): void; }'.

Check failure on line 216 in test/static/indexed.ts

View workflow job for this annotation

GitHub Actions / TypeBox (20.x, ubuntu-latest)

Property 'ToStatic' does not exist on type '{ ToStaticNever(): void; }'.
}
Expand Down

0 comments on commit 336a2ef

Please sign in to comment.