@@ -101,6 +101,11 @@ export abstract class TsDsl<T extends ts.Node = ts.Node> implements ITsDsl<T> {
101101 if ( typeof input === 'string' ) {
102102 return this . $expr ( input ) as NodeOfMaybe < I > ;
103103 }
104+ if ( typeof input === 'boolean' ) {
105+ return (
106+ input ? ts . factory . createTrue ( ) : ts . factory . createFalse ( )
107+ ) as NodeOfMaybe < I > ;
108+ }
104109 if ( input instanceof Array ) {
105110 return input . map ( ( item ) => this . _render ( item ) ) as NodeOfMaybe < I > ;
106111 }
@@ -113,29 +118,33 @@ export abstract class TsDsl<T extends ts.Node = ts.Node> implements ITsDsl<T> {
113118 const arr = input instanceof Array ? input : [ input ] ;
114119 return arr . map ( ( item ) => {
115120 const node =
116- typeof item === 'string'
117- ? ts . factory . createIdentifier ( item )
118- : this . _render ( item as any ) ;
121+ typeof item === 'string' ? this . $expr ( item ) : this . _render ( item as any ) ;
119122 return ts . isExpression ( node )
120123 ? ts . factory . createExpressionStatement ( node )
121124 : ( node as ts . Statement ) ;
122125 } ) ;
123126 }
124127
125- protected $type < I > ( type : I ) : TypeOfMaybe < I > {
126- if ( type === undefined ) {
128+ protected $type < I > (
129+ input : I ,
130+ args ?: ReadonlyArray < ts . TypeNode > ,
131+ ) : TypeOfMaybe < I > {
132+ if ( input === undefined ) {
127133 return undefined as TypeOfMaybe < I > ;
128134 }
129- if ( typeof type === 'string' ) {
130- return ts . factory . createTypeReferenceNode (
131- type ,
132- ) as unknown as TypeOfMaybe < I > ;
135+ if ( typeof input === 'string' ) {
136+ return ts . factory . createTypeReferenceNode ( input , args ) as TypeOfMaybe < I > ;
133137 }
134- if ( typeof type === 'boolean' ) {
135- const literal = type ? ts . factory . createTrue ( ) : ts . factory . createFalse ( ) ;
138+ if ( typeof input === 'boolean' ) {
139+ const literal = input
140+ ? ts . factory . createTrue ( )
141+ : ts . factory . createFalse ( ) ;
136142 return ts . factory . createLiteralTypeNode ( literal ) as TypeOfMaybe < I > ;
137143 }
138- return this . _render ( type as any ) as TypeOfMaybe < I > ;
144+ if ( input instanceof Array ) {
145+ return input . map ( ( item ) => this . $type ( item , args ) ) as TypeOfMaybe < I > ;
146+ }
147+ return this . _render ( input as any ) as TypeOfMaybe < I > ;
139148 }
140149
141150 private _render < T extends ts . Node > ( value : MaybeTsDsl < T > ) : T {
@@ -152,33 +161,51 @@ type NodeOf<I> =
152161 ? ReadonlyArray < U extends TsDsl < infer N > ? N : U >
153162 : I extends string
154163 ? ts . Expression
155- : I extends TsDsl < infer N >
156- ? N
157- : I extends ts . Node
158- ? I
159- : never ;
160-
161- type TypeOfMaybe < I > = undefined extends I
162- ? TypeOf < NonNullable < I > > | undefined
163- : TypeOf < I > ;
164-
165- type TypeOf < I > = I extends string
166- ? ts . TypeNode
167- : I extends boolean
168- ? ts . LiteralTypeNode
169- : I extends TsDsl < infer N >
170- ? N
171- : I extends ts . TypeNode
172- ? I
173- : never ;
164+ : I extends boolean
165+ ? ts . Expression
166+ : I extends TsDsl < infer N >
167+ ? N
168+ : I extends ts . Node
169+ ? I
170+ : never ;
174171
175172export type MaybeTsDsl < T > =
176- // if T includes string in the union
173+ // if T includes string
177174 string extends T
178175 ? Exclude < T , string > extends ts . Node
179176 ? string | Exclude < T , string > | TsDsl < Exclude < T , string > >
180177 : string
181- : // otherwise only node or DSL
182- T extends ts . Node
183- ? T | TsDsl < T >
184- : never ;
178+ : // if it's a DSL itself
179+ T extends TsDsl < any >
180+ ? T
181+ : // otherwise if it’s a Node
182+ T extends ts . Node
183+ ? T | TsDsl < T >
184+ : never ;
185+
186+ export type TypeOfTsDsl < T > = T extends TsDsl < infer U > ? U : never ;
187+
188+ export abstract class TypeTsDsl <
189+ T extends
190+ | ts . TypeNode
191+ | ts . TypeElement
192+ | ts . LiteralTypeNode
193+ | ts . TypeParameterDeclaration = ts . TypeNode ,
194+ > extends TsDsl < T > { }
195+
196+ type TypeOfMaybe < I > = undefined extends I
197+ ? TypeOf < NonNullable < I > > | undefined
198+ : TypeOf < I > ;
199+
200+ type TypeOf < I > =
201+ I extends ReadonlyArray < infer U >
202+ ? ReadonlyArray < TypeOf < U > >
203+ : I extends string
204+ ? ts . TypeNode
205+ : I extends boolean
206+ ? ts . LiteralTypeNode
207+ : I extends TsDsl < infer N >
208+ ? N
209+ : I extends ts . TypeNode
210+ ? I
211+ : never ;
0 commit comments