Skip to content

Commit 7b45ec1

Browse files
Support Inference of Ref inside Recursive inside Module (#1263)
1 parent 5a54314 commit 7b45ec1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/type/module/infer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { TRef } from '../ref/index'
4343
import { TTuple } from '../tuple/index'
4444
import { TUnion } from '../union/index'
4545
import { Static } from '../static/index'
46+
import { TRecursive } from '../recursive/index'
4647

4748
// ------------------------------------------------------------------
4849
// Array
@@ -160,6 +161,7 @@ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (
160161
Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> :
161162
Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : // intercept enum before union
162163
Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> :
164+
Type extends TRecursive<infer Schema extends TSchema> ? TInfer<ModuleProperties, Schema> :
163165
Static<Type>
164166
)
165167
// ------------------------------------------------------------------

test/static/import.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,26 @@ import { Type, Static } from '@sinclair/typebox'
130130
x?: null[],
131131
}>()
132132
}
133+
// ------------------------------------------------------------------
134+
// Ref inside Recursive
135+
// ------------------------------------------------------------------
136+
// prettier-ignore
137+
{
138+
const Module = Type.Module({
139+
T: Type.Recursive((_) =>
140+
Type.Object({
141+
M: Type.Ref("U"),
142+
})
143+
),
144+
U: Type.Union([
145+
Type.Literal("A"),
146+
Type.Literal("B")
147+
]),
148+
});
149+
150+
const T = Module.Import("T");
151+
type T = Static<typeof T>;
152+
Expect(T).ToStatic<{
153+
M: 'A'|'B'
154+
}>();
155+
}

0 commit comments

Comments
 (0)