Skip to content

Commit

Permalink
Reimplement Indexed Accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 26, 2023
1 parent e8e453c commit a8e3f83
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 109 deletions.
4 changes: 3 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Type } from '@sinclair/typebox'

const A = Type.Extends(Type.String(), Type.Number(), Type.Literal(1), Type.Literal(2))
const T = Type.TemplateLiteral('${A|B|C}')

const A = Type.Extract(T, Type.Literal('D'))
73 changes: 73 additions & 0 deletions src/type/resolve/exclude/exclude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*--------------------------------------------------------------------------
@sinclair/typebox
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Type, TSchema, Static, TTemplateLiteral, TLiteral, TNever, TUnion } from '../../../typebox'
import { TypeGuard } from '../../guard/index'
import { UnionResolver } from '../union/index'
import { ExtendsCheck, ExtendsResult } from '../extends/index'
import { TemplateLiteralToUnion } from '../template-literal/index'

// prettier-ignore
export namespace ExcludeResolver {
// ------------------------------------------------------------------
// Utility
// ------------------------------------------------------------------
export type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never
export type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never
export type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L]
export type AssertRest<T, E extends TSchema[] = TSchema[]> = T extends E ? T : []
export type AssertType<T, E extends TSchema = TSchema> = T extends E ? T : TNever
export type Assert<T, E> = T extends E ? T : never
// ------------------------------------------------------------------
// Resolve
// ------------------------------------------------------------------
export type TExcludeTemplateLiteralResult<T extends string> = UnionResolver.Resolve<AssertRest<UnionToTuple<{ [K in T]: TLiteral<K> }[T]>>>
export type TExcludeTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Exclude<Static<T>, Static<U>> extends infer S ? TExcludeTemplateLiteralResult<Assert<S, string>> : never
export type TExcludeArray<T extends TSchema[], U extends TSchema> = AssertRest<UnionToTuple<{
[K in keyof T]: Static<AssertType<T[K]>> extends Static<U> ? never : T[K]
}[number]>> extends infer R extends TSchema[] ? UnionResolver.Resolve<R> : never

export type Resolve<T extends TSchema, U extends TSchema> =
T extends TTemplateLiteral ? TExcludeTemplateLiteral<T, U> :
T extends TUnion<infer S> ? TExcludeArray<S, U> :
T extends U
? TNever
: T
export function Resolve<L extends TSchema, R extends TSchema>(L: L, R: R): Resolve<L, R> {
return (
TypeGuard.TTemplateLiteral(L) ? Resolve(TemplateLiteralToUnion.Resolve(L), R) :
TypeGuard.TTemplateLiteral(R) ? Resolve(L, TemplateLiteralToUnion.Resolve(R)) :
TypeGuard.TUnion(L) ? (() => {
const narrowed = L.anyOf.filter((inner) => ExtendsCheck.Check(inner, R) === ExtendsResult.False)
return (narrowed.length === 1 ? narrowed[0] : Type.Union(narrowed))
})() :
ExtendsCheck.Check(L, R) !== ExtendsResult.False ? Type.Never() :
L
) as Resolve<L, R>
}
}
29 changes: 29 additions & 0 deletions src/type/resolve/exclude/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@sinclair/typebox
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './exclude'
1 change: 1 addition & 0 deletions src/type/resolve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from './awaited/index'
export * from './composite/index'
export * from './discard/index'
export * from './distinct/index'
export * from './exclude/index'
export * from './extends/index'
export * from './extract/index'
export * from './increment/index'
Expand Down
2 changes: 1 addition & 1 deletion src/type/resolve/indexed/indexed-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export namespace IndexedTypeResolver {
)
export function Resolve<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): Resolve<T, K> {
return (
UnionResolver.Resolve(Keys(T, K))
UnionResolver.Resolve(Keys(T, K as PropertyKey[]))
) as Resolve<T, K>
}
}
2 changes: 1 addition & 1 deletion src/type/resolve/union/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export namespace UnionResolver {
T extends [TSchema] ? T[0] :
Modifiers.ResolveOptionalUnion<T>
)
export function Resolve<T extends TSchema[]>(T: T): Resolve<T> {
export function Resolve<T extends TSchema[]>(T: [...T]): Resolve<T> {
return (
T.length === 0 ? Type.Never() :
T.length === 1 ? T[0] :
Expand Down
Loading

0 comments on commit a8e3f83

Please sign in to comment.