Skip to content

Commit d34981b

Browse files
committed
Inference Stack
1 parent 08c6311 commit d34981b

19 files changed

Lines changed: 109 additions & 110 deletions

example/index.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
11
import Type, { type Static } from 'typebox'
2-
import { Compile } from 'typebox/compile'
3-
import Format from 'typebox/format'
42
import Value from 'typebox/value'
53

6-
// ------------------------------------------------------------------
7-
// Type
8-
// ------------------------------------------------------------------
9-
const T = Type.Object({
10-
x: Type.Number(),
11-
y: Type.Number(),
12-
z: Type.Number()
4+
// type JsonValue = ( // Type alias 'JsonValue' circularly references itself.
5+
// | JsonValue[]
6+
// | Record<string, JsonValue> // fix: Use Record<string, unknown>
7+
// | string
8+
// | number
9+
// | boolean
10+
// | null
11+
// )
12+
13+
14+
type A = Static<typeof JsonObject>
15+
16+
const { JsonObject, JsonValue, JsonArray } = Type.Module({
17+
JsonArray: Type.Array(Type.Ref('JsonValue')),
18+
JsonObject: Type.Record(Type.String(), Type.Ref('JsonValue')),
19+
JsonValue: Type.Union([
20+
Type.Ref('JsonObject'),
21+
Type.Ref('JsonArray'),
22+
Type.String(),
23+
Type.Number(),
24+
Type.Boolean(),
25+
Type.Null(),
26+
])
1327
})
1428

15-
// ------------------------------------------------------------------
16-
// Script
17-
// ------------------------------------------------------------------
18-
const S = Type.Script({ T }, `{
19-
[K in keyof T]: T[K] | null
20-
}`)
2129

22-
// ------------------------------------------------------------------
23-
// Infer
24-
// ------------------------------------------------------------------
25-
type T = Static<typeof T>
26-
type S = Static<typeof S>
27-
28-
// ------------------------------------------------------------------
29-
// Parse
30-
// ------------------------------------------------------------------
31-
32-
const R = Value.Parse(T, { x: 1, y: 2, z: 3 })
33-
34-
// ------------------------------------------------------------------
35-
// Compile
36-
// ------------------------------------------------------------------
37-
38-
const C = Compile(S)
39-
40-
const X = C.Parse({ x: 1, y: 2, z: 3 })
41-
42-
// ------------------------------------------------------------------
43-
// Format
44-
// ------------------------------------------------------------------
45-
46-
const E = Format.IsEmail('user@domain.com')

src/type/types/_codec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ import { type TProperties } from './properties.ts'
3535
// ------------------------------------------------------------------
3636
// Static
3737
// ------------------------------------------------------------------
38-
export type StaticCodec<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema, Decoded extends unknown> = Direction extends 'Decode' ? Decoded
39-
: StaticType<Direction, Context, This, Type>
38+
export type StaticCodec<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema, Decoded extends unknown> = Direction extends 'Decode' ? Decoded
39+
: StaticType<Stack, Direction, Context, This, Type>
4040

4141
// ------------------------------------------------------------------
4242
// Type
4343
// ------------------------------------------------------------------
44-
export type TDecodeCallback<Type extends TSchema, Decoded = unknown> = (input: StaticType<'Decode', {}, {}, Type>) => Decoded
45-
export type TEncodeCallback<Type extends TSchema, Decoded = unknown> = (input: Decoded) => StaticType<'Decode', {}, {}, Type>
44+
export type TDecodeCallback<Type extends TSchema, Decoded = unknown> = (input: StaticType<[], 'Decode', {}, {}, Type>) => Decoded
45+
export type TEncodeCallback<Type extends TSchema, Decoded = unknown> = (input: Decoded) => StaticType<[], 'Decode', {}, {}, Type>
4646
export type TCodec<Type extends TSchema = TSchema, Decoded extends unknown = unknown> = Type & {
4747
'~codec': {
4848
encode: TDecodeCallback<Type, Decoded>

src/type/types/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { type TProperties } from './properties.ts'
3636
// ------------------------------------------------------------------
3737
// Static
3838
// ------------------------------------------------------------------
39-
export type StaticArray<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40-
Result = StaticType<Direction, Context, This, Type>[]
39+
export type StaticArray<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40+
Result extends unknown[] = StaticType<Stack, Direction, Context, This, Type>[]
4141
> = Result
4242
// ------------------------------------------------------------------
4343
// Type

src/type/types/async-iterator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { type TProperties } from './properties.ts'
3636
// ------------------------------------------------------------------
3737
// Static
3838
// ------------------------------------------------------------------
39-
export type StaticAsyncIterator<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40-
Result = AsyncIterableIterator<StaticType<Direction, Context, This, Type>>
39+
export type StaticAsyncIterator<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40+
Result = AsyncIterableIterator<StaticType<Stack, Direction, Context, This, Type>>
4141
> = Result
4242
// ------------------------------------------------------------------
4343
// Type

src/type/types/constructor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import { type TProperties } from './properties.ts'
3737
// ------------------------------------------------------------------
3838
// Static
3939
// ------------------------------------------------------------------
40-
export type StaticConstructor<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema,
41-
StaticParameters extends unknown[] = StaticInstantiatedParameters<Direction, Context, This, Parameters>,
42-
StaticReturnType extends unknown = StaticType<Direction, Context, This, InstanceType>,
40+
export type StaticConstructor<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema,
41+
StaticParameters extends unknown[] = StaticInstantiatedParameters<Stack, Direction, Context, This, Parameters>,
42+
StaticReturnType extends unknown = StaticType<Stack, Direction, Context, This, InstanceType>,
4343
Result = new (...args: StaticParameters) => StaticReturnType
4444
> = Result
4545
// ------------------------------------------------------------------

src/type/types/cyclic.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ import { type TProperties } from './properties.ts'
3737
// ------------------------------------------------------------------
3838
// Static
3939
// ------------------------------------------------------------------
40-
export type StaticCyclic<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Defs extends TProperties, Ref extends string,
41-
Result extends unknown = Ref extends keyof Defs
42-
? StaticType<Direction, Defs, This, Defs[Ref]>
43-
: never
40+
export type StaticCyclic<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Defs extends TProperties, Ref extends string,
41+
Result extends unknown = (
42+
Ref extends keyof Defs
43+
? StaticType<[...Stack, Ref], Direction, Defs, This, Defs[Ref]>
44+
: never
45+
)
4446
> = Result
4547
// ------------------------------------------------------------------
4648
// Type

src/type/types/function.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ import { type TInstantiate } from '../engine/instantiate.ts'
4444
// performs a deferred Static Instantiate for inference only.
4545
//
4646
// ------------------------------------------------------------------
47-
export type StaticInstantiatedParameters<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[],
47+
export type StaticInstantiatedParameters<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[],
4848
Evaluated extends TSchema = TInstantiate<Context, TTuple<Parameters>>,
49-
Static extends unknown = StaticType<Direction, Context, This, Evaluated>,
49+
Static extends unknown = StaticType<Stack, Direction, Context, This, Evaluated>,
5050
Result extends unknown[] = Static extends unknown[] ? Static : []
5151
> = Result
5252

5353
// ------------------------------------------------------------------
5454
// Static
5555
// ------------------------------------------------------------------
56-
export type StaticFunction<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema,
57-
StaticParameters extends unknown[] = StaticInstantiatedParameters<Direction, Context, This, Parameters>,
58-
StaticReturnType extends unknown = StaticType<Direction, Context, This, ReturnType>,
56+
export type StaticFunction<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema,
57+
StaticParameters extends unknown[] = StaticInstantiatedParameters<Stack, Direction, Context, This, Parameters>,
58+
StaticReturnType extends unknown = StaticType<Stack, Direction, Context, This, ReturnType>,
5959
Result = (...args: StaticParameters) => StaticReturnType
6060
> = Result
6161
// ------------------------------------------------------------------

src/type/types/intersect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import { type TProperties } from './properties.ts'
3636
// ------------------------------------------------------------------
3737
// Static
3838
// ------------------------------------------------------------------
39-
export type StaticIntersect<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (
39+
export type StaticIntersect<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (
4040
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
41-
? StaticIntersect<Direction, Context, This, Right, Result & StaticType<Direction, Context, This, Left>>
41+
? StaticIntersect<Stack, Direction, Context, This, Right, Result & StaticType<Stack, Direction, Context, This, Left>>
4242
: Result
4343
)
4444
// ------------------------------------------------------------------

src/type/types/iterator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { type TProperties } from './properties.ts'
3636
// ------------------------------------------------------------------
3737
// Static
3838
// ------------------------------------------------------------------
39-
export type StaticIterator<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40-
Result = IterableIterator<StaticType<Direction, Context, This, Type>>
39+
export type StaticIterator<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema,
40+
Result = IterableIterator<StaticType<Stack, Direction, Context, This, Type>>
4141
> = Result
4242
// ------------------------------------------------------------------
4343
// Type

src/type/types/object.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { type TProperties, type TRequiredArray, type StaticProperties, RequiredA
3636
// ------------------------------------------------------------------
3737
// Static
3838
// ------------------------------------------------------------------
39-
export type StaticObject<Direction extends StaticDirection, Context extends TProperties, _This extends TProperties, Properties extends TProperties,
40-
Result = keyof Properties extends never ? object : StaticProperties<Direction, Context, Properties, Properties>
39+
export type StaticObject<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, _This extends TProperties, Properties extends TProperties,
40+
Result = keyof Properties extends never ? object : StaticProperties<Stack, Direction, Context, Properties, Properties>
4141
> = Result
4242
// ------------------------------------------------------------------
4343
// Schema

0 commit comments

Comments
 (0)