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 d58554c commit 507583e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
18 changes: 12 additions & 6 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import {
} from '@sinclair/typebox'
import { TObject, TUnion, UnionToTuple, TInteger, TBigInt, TIntersect, TString, TNumber, TBoolean, TNever, TTuple, UnionType, TRecursive } from '@sinclair/typebox'

const T = Type.Intersect([Type.Object({ x: Type.Optional(Type.Number()) }), Type.Object({ x: Type.Optional(Type.Number()) })])

const S = Type.Intersect([Type.Number(), Type.Object({ x: Type.Number() })])

type A = Static<typeof S>

const I = Type.Index(T, Type.KeyOf(T))
const I = Type.Intersect([
Type.Intersect([
Type.Object({ x: Type.String(), y: Type.Literal(1) }),
Type.Object({ x: Type.String(), y: Type.Number() })
]),
Type.Intersect([
Type.Object({ x: Type.String(), y: Type.Number() }),
Type.Object({ x: Type.String(), y: Type.Union([Type.Literal('A'), Type.Literal('B')]) })
])
])

const R = Type.Index(I, ['x', 'y'])

// prettier-ignore
// export type UnionTypeIsOptional<T extends TSchema[]> =
Expand Down
25 changes: 20 additions & 5 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ export type OptionalUnwrapRest<T extends TSchema[]> =
? [OptionalUnwrapType<S>, ...OptionalUnwrapRest<R>]
: [L, ...OptionalUnwrapRest<R>]
: []
// ------------------------------------------------------------------
// DistinctRest
// ------------------------------------------------------------------
export type DistinctRestIncludes<T extends TSchema[], C extends TSchema> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? C extends L
? true
: DistinctRestIncludes<R, C>
: false
export type DistinctRest<T extends TSchema[], Acc extends TSchema[] = []> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? DistinctRestIncludes<Acc, L> extends false
? DistinctRest<R, [...Acc, L]>
: DistinctRest<R, [...Acc]>
: Acc
// --------------------------------------------------------------------------
// IntersectType
// --------------------------------------------------------------------------
Expand All @@ -112,8 +127,8 @@ export type IntersectTypeIsOptional<T extends TSchema[]> =
// prettier-ignore
export type IntersectTypeOptional<T extends TSchema[], U extends TSchema[] = OptionalUnwrapRest<T>> =
IntersectTypeIsOptional<T> extends true
? TOptional<TIntersect<U>>
: TIntersect<U>
? TOptional<TIntersect<DistinctRest<U>>>
: TIntersect<DistinctRest<U>>
// prettier-ignore
export type IntersectType<T extends TSchema[]> =
T extends [] ? TNever :
Expand All @@ -132,8 +147,8 @@ export type UnionTypeIsOptional<T extends TSchema[]> =
// prettier-ignore
export type UnionTypeOptional<T extends TSchema[], U extends TSchema[] = OptionalUnwrapRest<T>> =
UnionTypeIsOptional<T> extends true
? TOptional<TUnion<U>>
: TUnion<U>
? TOptional<TUnion<DistinctRest<U>>>
: TUnion<DistinctRest<U>>
// prettier-ignore
export type UnionType<T extends TSchema[]> =
T extends [] ? TNever :
Expand Down Expand Up @@ -487,7 +502,7 @@ export type TIndexKey<T extends TSchema, K extends PropertyKey, R = TIndexKeyRes
// prettier-ignore
export type TIndexKeysResolve<T extends TSchema, K extends PropertyKey[]> =
K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]]
? [...TIndexKeyResolve<T, L>, ...TIndexKeysResolve<T, R>]
? [TIndexKey<T, L>, ...TIndexKeysResolve<T, R>]
: []
// prettier-ignore
export type TIndexKeys<T extends TSchema, K extends PropertyKey[], R = TIndexKeysResolve<T, K>> =
Expand Down

0 comments on commit 507583e

Please sign in to comment.