-
Notifications
You must be signed in to change notification settings - Fork 9
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
00b44d4
commit e202826
Showing
8 changed files
with
119 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as utils from '../utils' | ||
import * as types from './typings' | ||
|
||
type _InferTuple<T extends types.JexType[]> = T extends [] | ||
? [] | ||
: T extends [infer A] | ||
? [JexInfer<utils.types.Cast<A, types.JexType>>] | ||
: T extends [infer A, ...infer B] | ||
? [JexInfer<utils.types.Cast<A, types.JexType>>, ..._InferTuple<utils.types.Cast<B, types.JexType[]>>] | ||
: T | ||
|
||
export type JexInfer<J extends types.JexType> = | ||
J extends types.JexStringLiteral<infer V> | ||
? V | ||
: J extends types.JexNumberLiteral<infer V> | ||
? V | ||
: J extends types.JexBooleanLiteral<infer V> | ||
? V | ||
: J extends types.JexString | ||
? string | ||
: J extends types.JexNumber | ||
? number | ||
: J extends types.JexBoolean | ||
? boolean | ||
: J extends types.JexNull | ||
? null | ||
: J extends types.JexUndefined | ||
? undefined | ||
: J extends types.JexUnion | ||
? JexInfer<J['anyOf'][number]> | ||
: J extends types.JexObject | ||
? { [K in keyof J['properties']]: JexInfer<J['properties'][K]> } | ||
: J extends types.JexArray | ||
? JexInfer<J['items']>[] | ||
: J extends types.JexMap | ||
? { [key: string]: JexInfer<J['items']> } | ||
: J extends types.JexAny | ||
? any | ||
: J extends types.JexTuple | ||
? _InferTuple<J['items']> | ||
: never |
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,36 @@ | ||
import * as utils from '../utils' | ||
import { $ } from './jex-builder' | ||
import { JexInfer } from './jex-infer' | ||
|
||
const _any = $.any() | ||
const _string = $.string() | ||
const _number = $.number() | ||
const _boolean = $.boolean() | ||
const _null = $.null() | ||
const _undefined = $.undefined() | ||
const _literalStr = $.literal('apple') | ||
const _literalNum = $.literal(1) | ||
const _literalBool = $.literal(true) | ||
const _object = $.object({ a: $.string(), b: $.number() }) | ||
const _array = $.array($.string()) | ||
const _map = $.map($.string()) | ||
const _tuple = $.tuple([$.string(), $.number()]) | ||
const _union = $.union([$.string(), $.number()]) | ||
const _complex = $.array($.union([$.map($.tuple([$.string(), $.number()])), $.object({ a: $.array($.string()) })])) | ||
|
||
type ExpectedComplex = (Record<string, [string, number]> | { a: string[] })[] | ||
type _Any = utils.types.Expect<utils.types.Equals<JexInfer<typeof _any>, any>> | ||
type _String = utils.types.Expect<utils.types.Equals<JexInfer<typeof _string>, string>> | ||
type _Number = utils.types.Expect<utils.types.Equals<JexInfer<typeof _number>, number>> | ||
type _Boolean = utils.types.Expect<utils.types.Equals<JexInfer<typeof _boolean>, boolean>> | ||
type _Null = utils.types.Expect<utils.types.Equals<JexInfer<typeof _null>, null>> | ||
type _Undefined = utils.types.Expect<utils.types.Equals<JexInfer<typeof _undefined>, undefined>> | ||
type _LiteralStr = utils.types.Expect<utils.types.Equals<JexInfer<typeof _literalStr>, 'apple'>> | ||
type _LiteralNum = utils.types.Expect<utils.types.Equals<JexInfer<typeof _literalNum>, 1>> | ||
type _LiteralBool = utils.types.Expect<utils.types.Equals<JexInfer<typeof _literalBool>, true>> | ||
type _Object = utils.types.Expect<utils.types.Equals<JexInfer<typeof _object>, { a: string; b: number }>> | ||
type _Array = utils.types.Expect<utils.types.Equals<JexInfer<typeof _array>, string[]>> | ||
type _Map = utils.types.Expect<utils.types.Equals<JexInfer<typeof _map>, Record<string, string>>> | ||
type _Tuple = utils.types.Expect<utils.types.Equals<JexInfer<typeof _tuple>, [string, number]>> | ||
type _Union = utils.types.Expect<utils.types.Equals<JexInfer<typeof _union>, string | number>> | ||
type _Complex = utils.types.Expect<utils.types.Equals<JexInfer<typeof _complex>, ExpectedComplex>> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * as collection from './collection-utils' | ||
export * as typings from './type-utils' | ||
export * as types from './type-utils' |
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