-
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
e8e453c
commit a8e3f83
Showing
8 changed files
with
144 additions
and
109 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,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')) |
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 |
---|---|---|
@@ -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> | ||
} | ||
} |
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 |
---|---|---|
@@ -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' |
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
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.