@@ -26,76 +26,96 @@ THE SOFTWARE.
2626
2727---------------------------------------------------------------------------*/
2828
29+ import { CreateType } from '../create/type'
30+ import { Ensure } from '../helpers/index'
2931import type { TSchema , SchemaOptions } from '../schema/index'
32+ import { Computed , type TComputed } from '../computed/index'
3033import { Intersect , type TIntersect } from '../intersect/index'
3134import { Union , type TUnion } from '../union/index'
3235import { type TPromise } from '../promise/index'
33- import { CreateType } from '../create/type '
36+ import { Ref , type TRef } from '../ref/index '
3437
3538// ------------------------------------------------------------------
3639// TypeGuard
3740// ------------------------------------------------------------------
38- import { IsIntersect , IsUnion , IsPromise } from '../guard/kind'
39- // ------------------------------------------------------------------
40- // FromRest
41- // ------------------------------------------------------------------
41+ import { IsIntersect , IsUnion , IsPromise , IsRef , IsComputed } from '../guard/kind'
42+
43+ // ----------------------------------------------------------------
44+ // FromComputed
45+ // ----------------------------------------------------------------
4246// prettier-ignore
43- type TFromRest < T extends TSchema [ ] , Acc extends TSchema [ ] = [ ] > =
44- T extends [ infer L extends TSchema , ...infer R extends TSchema [ ] ]
45- ? TFromRest < R , [ ...Acc , TAwaited < L > ] >
46- : Acc
47+ type TFromComputed < Target extends string , Parameters extends TSchema [ ] > = Ensure < (
48+ TComputed < 'Awaited' , [ TComputed < Target , Parameters > ] >
49+ ) >
50+ // prettier-ignore
51+ function FromComputed < Target extends string , Parameters extends TSchema [ ] > ( target : Target , parameters : Parameters ) : TFromComputed < Target , Parameters > {
52+ return Computed ( 'Awaited' , [ Computed ( target , parameters ) ] ) as never
53+ }
54+ // ----------------------------------------------------------------
55+ // Ref
56+ // ----------------------------------------------------------------
57+ type TFromRef < Ref extends string > = Ensure < TComputed < 'Awaited' , [ TRef < Ref > ] > >
4758// prettier-ignore
48- function FromRest < T extends TSchema [ ] > ( T : [ ... T ] ) : TFromRest < T > {
49- return T . map ( L => AwaitedResolve ( L ) ) as never
59+ function FromRef < Ref extends string > ( $ref : Ref ) : TFromRef < Ref > {
60+ return Computed ( 'Awaited' , [ Ref ( $ref ) ] ) as never
5061}
5162// ----------------------------------------------------------------
5263// FromIntersect
5364// ----------------------------------------------------------------
5465// prettier-ignore
55- type TFromIntersect < T extends TSchema [ ] > = TIntersect < TFromRest < T > >
66+ type TFromIntersect < Types extends TSchema [ ] > = (
67+ TIntersect < TFromRest < Types > >
68+ )
5669// prettier-ignore
57- function FromIntersect < T extends TSchema [ ] > ( T : [ ...T ] ) : TFromIntersect < T > {
58- return Intersect ( FromRest ( T ) as TSchema [ ] ) as never
70+ function FromIntersect < Types extends TSchema [ ] > ( types : [ ...Types ] ) : TFromIntersect < Types > {
71+ return Intersect ( FromRest ( types ) as TSchema [ ] ) as never
5972}
6073// ----------------------------------------------------------------
6174// FromUnion
6275// ----------------------------------------------------------------
6376// prettier-ignore
64- type TFromUnion < T extends TSchema [ ] > = TUnion < TFromRest < T > >
77+ type TFromUnion < Types extends TSchema [ ] > = TUnion < TFromRest < Types > >
6578// prettier-ignore
66- function FromUnion < T extends TSchema [ ] > ( T : [ ...T ] ) : TFromUnion < T > {
67- return Union ( FromRest ( T ) as TSchema [ ] ) as never
79+ function FromUnion < Types extends TSchema [ ] > ( types : [ ...Types ] ) : TFromUnion < Types > {
80+ return Union ( FromRest ( types ) as TSchema [ ] ) as never
6881}
6982// ----------------------------------------------------------------
7083// Promise
7184// ----------------------------------------------------------------
72- type TFromPromise < T extends TSchema > = TAwaited < T >
85+ type TFromPromise < Type extends TSchema > = TAwaited < Type >
7386// prettier-ignore
74- function FromPromise < T extends TSchema > ( T : T ) : TFromPromise < T > {
75- return AwaitedResolve ( T ) as never
87+ function FromPromise < Type extends TSchema > ( type : Type ) : TFromPromise < Type > {
88+ return Awaited ( type ) as never
7689}
77- // ----------------------------------------------------------------
78- // AwaitedResolve
79- // ----------------------------------------------------------------
90+ // ------------------------------------------------------------------
91+ // FromRest
92+ // ------------------------------------------------------------------
8093// prettier-ignore
81- function AwaitedResolve < T extends TSchema > ( T : T ) : TAwaited < T > {
82- return (
83- IsIntersect ( T ) ? FromIntersect ( T . allOf ) :
84- IsUnion ( T ) ? FromUnion ( T . anyOf ) :
85- IsPromise ( T ) ? FromPromise ( T . item ) :
86- T
87- ) as never
94+ type TFromRest < Types extends TSchema [ ] , Result extends TSchema [ ] = [ ] > = (
95+ Types extends [ infer Left extends TSchema , ...infer Right extends TSchema [ ] ]
96+ ? TFromRest < Right , [ ...Result , TAwaited < Left > ] >
97+ : Result
98+ )
99+ // prettier-ignore
100+ function FromRest < Types extends TSchema [ ] > ( types : [ ...Types ] ) : TFromRest < Types > {
101+ return types . map ( type => Awaited ( type ) ) as never
88102}
89103// ------------------------------------------------------------------
90104// TAwaited
91105// ------------------------------------------------------------------
92106// prettier-ignore
93- export type TAwaited < T extends TSchema > =
94- T extends TIntersect < infer S > ? TIntersect < TFromRest < S > > :
95- T extends TUnion < infer S > ? TUnion < TFromRest < S > > :
96- T extends TPromise < infer S > ? TAwaited < S > :
97- T
107+ export type TAwaited < Type extends TSchema > = (
108+ Type extends TComputed < infer Target extends string , infer Parameters extends TSchema [ ] > ? TFromComputed < Target , Parameters > :
109+ Type extends TRef < infer Ref extends string > ? TFromRef < Ref > :
110+ Type extends TIntersect < infer Types extends TSchema [ ] > ? TIntersect < TFromRest < Types > > :
111+ Type extends TUnion < infer Types extends TSchema [ ] > ? TUnion < TFromRest < Types > > :
112+ Type extends TPromise < infer Type extends TSchema > ? TAwaited < Type > :
113+ Type
114+ )
98115/** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
99- export function Awaited < T extends TSchema > ( T : T , options ?: SchemaOptions ) : TAwaited < T > {
100- return CreateType ( AwaitedResolve ( T ) , options ) as never
116+ export function Awaited < T extends TSchema > ( type : T , options ?: SchemaOptions ) : TAwaited < T > {
117+ return CreateType (
118+ IsComputed ( type ) ? FromComputed ( type . target , type . parameters ) : IsIntersect ( type ) ? FromIntersect ( type . allOf ) : IsUnion ( type ) ? FromUnion ( type . anyOf ) : IsPromise ( type ) ? FromPromise ( type . item ) : IsRef ( type ) ? FromRef ( type . $ref ) : type ,
119+ options ,
120+ ) as never
101121}
0 commit comments