-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaf9e78
commit 1f5b632
Showing
6 changed files
with
235 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,31 @@ | ||
import { Value } from '@sinclair/typebox/value' | ||
import { TSchema, Type, Evaluate } from '@sinclair/typebox' | ||
import { KeyOfStringResolver, IndexedTypeResolver, Logic as PropertySet } from '@sinclair/typebox/type' | ||
import { TSchema, Type, TypeGuard, Discard, Evaluate, Ensure, TObject, Kind } from '@sinclair/typebox' | ||
import { KeyOfStringResolver, IndexedTypeResolver, OmitResolver } from '@sinclair/typebox/type' | ||
|
||
export namespace OmitResolver { | ||
export type Evaluate<T> = T extends infer O ? { [K in keyof O]: O[K] } : never | ||
// ---------------------------------------------------------------- | ||
// Reduce | ||
// ---------------------------------------------------------------- | ||
export type Reduce<T extends TSchema, K extends PropertyKey[]> = K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? { [_ in L]: IndexedTypeResolver.Resolve<T, [L]> } & Reduce<T, R> : {} | ||
export function Reduce<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): Reduce<T, K> { | ||
return K.reduce((acc, key) => { | ||
return { ...acc, [key]: IndexedTypeResolver.Resolve(T, [key]) } | ||
}, {}) as Reduce<T, K> | ||
} | ||
// ---------------------------------------------------------------- | ||
// Resolve | ||
// ---------------------------------------------------------------- | ||
export type Resolve<T extends TSchema, K extends PropertyKey[], A extends PropertyKey[] = KeyOfStringResolver.Resolve<T>, B extends PropertyKey[] = PropertySet.Complement<A, K>> = Evaluate<Reduce<T, B>> | ||
// const R = TypeGuard.TUnion( | ||
// Type.Union([ | ||
// Type.Object({ | ||
// x: Type.Number(), | ||
// }), | ||
// {} as any, | ||
// ]), | ||
// ) | ||
console.log( | ||
TypeGuard.TSchema( | ||
Type.Object({ | ||
y: {} as any, | ||
}), | ||
), | ||
'?', | ||
) | ||
|
||
export function Resolve<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): Resolve<T, K> { | ||
const A = KeyOfStringResolver.Resolve(T) as PropertyKey[] | ||
const B = PropertySet.Complement(A, K) as PropertyKey[] | ||
return Reduce(T, B) as Resolve<T, K> | ||
} | ||
} | ||
// // @ts-ignore | ||
// Assert.IsEqual(T.properties.x, undefined) | ||
// // @ts-ignore | ||
// Assert.IsEqual(T.properties.y, undefined) | ||
// // @ts-ignore | ||
// Assert.IsEqual(T.required, undefined) | ||
|
||
const A = Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}) | ||
const B = Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}) | ||
const T = Type.Intersect([Type.Intersect([A, B]), Type.Intersect([A, B]), Type.Intersect([A, B])]) | ||
|
||
const R = OmitResolver.Resolve(T, ['x']) | ||
console.log(JSON.stringify(R, null, 2)) | ||
// const K = Type.TemplateLiteral('A${0|1}${0|1}${0|1}${0|1}${0|1}${0|1}${0|1}${0|1}') | ||
// const T = Type.Record(K, Type.Null()) | ||
// const O = Type.Omit(T, ['asd']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.