Skip to content

Commit a464a1d

Browse files
committed
Dependent If Type
1 parent 1d1006e commit a464a1d

8 files changed

Lines changed: 119 additions & 3 deletions

File tree

design/syntax/syntax.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ const Base = Runtime.Union([
283283
Runtime.Ref('Constructor'),
284284
Runtime.Ref('_Function_'),
285285
Runtime.Ref('Mapped'),
286+
Runtime.Ref('_If_'),
286287
Runtime.Ref('Options'),
287288
Runtime.Ref('GenericCall'),
288289
Runtime.Ref('Reference')
@@ -638,6 +639,14 @@ const Mapped = Runtime.Tuple([
638639
Runtime.Const(RBrace),
639640
])
640641
// ------------------------------------------------------------------
642+
// If
643+
// ------------------------------------------------------------------
644+
const _If_ = Runtime.Union([
645+
Runtime.Tuple([Runtime.Const('if'), Runtime.Ref('Type'), Runtime.Const('then'), Runtime.Ref('Type'), Runtime.Const('else'), Runtime.Ref('Type')]),
646+
Runtime.Tuple([Runtime.Const('if'), Runtime.Ref('Type'), Runtime.Const('then'), Runtime.Ref('Type')]),
647+
Runtime.Tuple([Runtime.Const('if'), Runtime.Ref('Type'), Runtime.Const('else'), Runtime.Ref('Type')])
648+
])
649+
// ------------------------------------------------------------------
641650
// Options
642651
// ------------------------------------------------------------------
643652
const Options = Runtime.Tuple([
@@ -1008,6 +1017,9 @@ export const SyntaxModule = new Runtime.Module({
10081017
MappedOptional,
10091018
MappedAs,
10101019
Mapped,
1020+
1021+
_If_,
1022+
10111023
Reference,
10121024
Options,
10131025

src/type/engine/instantiate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { type TDeferred, Deferred, IsDeferred } from '../types/deferred.ts'
5050
import { type TFunction, _Function_, IsFunction, FunctionOptions } from '../types/function.ts'
5151
import { type TCall, IsCall } from '../types/call.ts'
5252
import { type TIdentifier } from '../types/identifier.ts'
53+
import { type TIf, If, IsIf, IfOptions } from '../types/if.ts'
5354
import { type TIntersect, Intersect, IsIntersect, IntersectOptions } from '../types/intersect.ts'
5455
import { type TIterator, Iterator, IsIterator, IteratorOptions } from '../types/iterator.ts'
5556
import { type TObject, Object, IsObject, ObjectOptions } from '../types/object.ts'
@@ -305,9 +306,10 @@ export type TInstantiateType<Context extends TProperties, State extends TState,
305306
Type extends TArray<infer Type extends TSchema> ? TArray<TInstantiateType<Context, State, Type>> :
306307
Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TInstantiateType<Context, State, Type>> :
307308
Type extends TCall<infer Target extends TSchema, infer Parameters extends TSchema[]> ? TCallInstantiate<Context, State, Target, Parameters> :
308-
Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TInstantiateTypes<Context, State,Parameters>, TInstantiateType<Context, State,InstanceType>> :
309+
Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TInstantiateTypes<Context, State,Parameters>, TInstantiateType<Context, State, InstanceType>> :
309310
Type extends TDeferred<infer Action extends string, infer Types extends TSchema[]> ? TInstantiateDeferred<Context, State, Action, Types> :
310311
Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TInstantiateTypes<Context, State, Parameters>, TInstantiateType<Context, State,ReturnType>> :
312+
Type extends TIf<infer If extends TSchema, infer Then extends TSchema, infer Else extends TSchema> ? TIf<TInstantiateType<Context, State, If>, TInstantiateType<Context, State, Then>, TInstantiateType<Context, State, Else>> :
311313
Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TInstantiateTypes<Context, State, Types>> :
312314
Type extends TIterator<infer Type extends TSchema> ? TIterator<TInstantiateType<Context, State, Type>> :
313315
Type extends TObject<infer Properties extends TProperties> ? TObject<TInstantiateProperties<Context, State, Properties>> :
@@ -338,6 +340,7 @@ export function InstantiateType<Context extends TProperties, State extends TStat
338340
IsConstructor(type) ? Constructor(InstantiateTypes(context, state, type.parameters), InstantiateType(context, state, type.instanceType) as never, ConstructorOptions(type)) :
339341
IsDeferred(type) ? InstantiateDeferred(context, state, type.action, type.parameters, type.options) :
340342
IsFunction(type) ? _Function_(InstantiateTypes(context, state, type.parameters), InstantiateType(context, state, type.returnType) as never, FunctionOptions(type)) :
343+
IsIf(type) ? If(InstantiateType(context, state, type.if), InstantiateType(context, state, type.then), InstantiateType(context, state, type.else), IfOptions(type)) :
341344
IsIntersect(type) ? Intersect(InstantiateTypes(context, state, type.allOf), IntersectOptions(type)) :
342345
IsIterator(type) ? Iterator(InstantiateType(context, state, type.iteratorItems), IteratorOptions(type)) :
343346
IsObject(type) ? Object(InstantiateProperties(context, state, type.properties), ObjectOptions(type)) :

src/type/script/mapping.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,25 @@ export function MappedMapping(input: [unknown, unknown, unknown, unknown, unknow
12081208
)
12091209
}
12101210
// -------------------------------------------------------------------
1211+
// _If_: ['if', Type, 'then', Type, 'else', Type] | ['if', Type, 'then', Type] | ['if', Type, 'else', Type]
1212+
// -------------------------------------------------------------------
1213+
export type T_If_Mapping<Input extends [unknown, unknown, unknown, unknown, unknown, unknown] | [unknown, unknown, unknown, unknown]> = (
1214+
Input extends ['if', infer If extends T.TSchema, 'then', infer Then extends T.TSchema, 'else', infer Else extends T.TSchema]
1215+
? T.TIf<If, Then, Else> :
1216+
Input extends ['if', infer If extends T.TSchema, 'then', infer Then extends T.TSchema]
1217+
? T.TIf<If, Then, T.TUnknown> :
1218+
Input extends ['if', infer If extends T.TSchema, 'else', infer Else extends T.TSchema]
1219+
? T.TIf<If, T.TUnknown, Else> :
1220+
never
1221+
)
1222+
export function _If_Mapping(input: [unknown, unknown, unknown, unknown, unknown, unknown] | [unknown, unknown, unknown, unknown]): unknown {
1223+
return (
1224+
Guard.IsEqual(input.length, 6) ? T.If(input[1] as T.TSchema, input[3] as T.TSchema, input[5] as T.TSchema) :
1225+
Guard.IsEqual(input[2], 'then') ? T.If(input[1] as T.TSchema, input[3] as T.TSchema, T.Unknown()) :
1226+
T.If(input[1] as T.TSchema, T.Unknown(), input[3] as T.TSchema)
1227+
)
1228+
}
1229+
// -------------------------------------------------------------------
12111230
// Reference: <Ident>
12121231
// -------------------------------------------------------------------
12131232
export type TReferenceMapping<Input extends string,

0 commit comments

Comments
 (0)