From 507583e3b760b5f98d7d10c332ddb856a6f2559f Mon Sep 17 00:00:00 2001 From: sinclair Date: Thu, 23 Nov 2023 02:02:13 +0900 Subject: [PATCH] Reimplement Index Types --- examples/index.ts | 18 ++++++++++++------ src/typebox.ts | 25 ++++++++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/examples/index.ts b/examples/index.ts index e75241a79..f682e9b9f 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -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 - -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 = diff --git a/src/typebox.ts b/src/typebox.ts index 9476d489f..329e82b2e 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -99,6 +99,21 @@ export type OptionalUnwrapRest = ? [OptionalUnwrapType, ...OptionalUnwrapRest] : [L, ...OptionalUnwrapRest] : [] +// ------------------------------------------------------------------ +// DistinctRest +// ------------------------------------------------------------------ +export type DistinctRestIncludes = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? C extends L + ? true + : DistinctRestIncludes + : false +export type DistinctRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? DistinctRestIncludes extends false + ? DistinctRest + : DistinctRest + : Acc // -------------------------------------------------------------------------- // IntersectType // -------------------------------------------------------------------------- @@ -112,8 +127,8 @@ export type IntersectTypeIsOptional = // prettier-ignore export type IntersectTypeOptional> = IntersectTypeIsOptional extends true - ? TOptional> - : TIntersect + ? TOptional>> + : TIntersect> // prettier-ignore export type IntersectType = T extends [] ? TNever : @@ -132,8 +147,8 @@ export type UnionTypeIsOptional = // prettier-ignore export type UnionTypeOptional> = UnionTypeIsOptional extends true - ? TOptional> - : TUnion + ? TOptional>> + : TUnion> // prettier-ignore export type UnionType = T extends [] ? TNever : @@ -487,7 +502,7 @@ export type TIndexKey = K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] - ? [...TIndexKeyResolve, ...TIndexKeysResolve] + ? [TIndexKey, ...TIndexKeysResolve] : [] // prettier-ignore export type TIndexKeys> =