Skip to content

Commit 1773b2e

Browse files
authored
Version 1.0.70 (#1498)
* Boolean Static * Static Record Template Literal Key * ChangeLog * Version
1 parent 97f80db commit 1773b2e

6 files changed

Lines changed: 91 additions & 16 deletions

File tree

changelog/1.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.0.70](https://github.com/sinclairzx81/typebox/pull/1498)
7+
- Static Boolean Schematics | Static Template Literal Record Key
68
- [Revision 1.0.69](https://github.com/sinclairzx81/typebox/pull/1493)
79
- Optimize Union Evaluation for Distinct Sets
810
- [Revision 1.0.68](https://github.com/sinclairzx81/typebox/pull/1492)

src/schema/static/schema.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,24 @@ type TKeywordsEvaluated<Schema extends unknown,
9898
: Schema
9999
> = Result
100100
// ------------------------------------------------------------------
101-
// XStatic
101+
// XStaticObject
102102
// ------------------------------------------------------------------
103-
export type XStaticSchema<Stack extends string[], Root extends XSchema, Schema extends XSchema,
103+
export type XStaticObject<Stack extends string[], Root extends XSchema, Schema extends XSchema,
104104
Keywords extends unknown[] = TFromKeywords<Stack, Root, Schema>,
105105
Intersected extends unknown = TKeywordsIntersected<Keywords>,
106106
Evaluated extends unknown = TKeywordsEvaluated<Intersected>
107-
> = Evaluated
107+
> = Evaluated
108+
// ------------------------------------------------------------------
109+
// XStaticBoolean
110+
// ------------------------------------------------------------------
111+
export type XStaticBoolean<Schema extends boolean,
112+
Result extends unknown = Schema extends false ? never : unknown
113+
> = Result
114+
// ------------------------------------------------------------------
115+
// XStaticSchema
116+
// ------------------------------------------------------------------
117+
export type XStaticSchema<Stack extends string[], Root extends XSchema, Schema extends XSchema,
118+
Result extends unknown = Schema extends boolean
119+
? XStaticBoolean<Schema>
120+
: XStaticObject<Stack, Root, Schema>
121+
> = Result

src/type/types/record.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,30 @@ import { Memory } from '../../system/memory/index.ts'
3232
import { Guard } from '../../guard/index.ts'
3333
import { type TSchema, type TObjectOptions, IsKind } from './schema.ts'
3434
import { type StaticType, type StaticDirection } from './static.ts'
35-
3635
import { type TProperties } from './properties.ts'
3736
import { type TInteger, Integer, IntegerPattern } from './integer.ts'
38-
39-
import { type TTemplateLiteral } from './template-literal.ts'
4037
import { type TNumber, Number, NumberPattern } from './number.ts'
4138
import { type TString, String, StringPattern } from './string.ts'
42-
4339
import { type TDeferred, Deferred } from './deferred.ts'
40+
4441
import { type TInstantiate, Instantiate } from '../engine/instantiate.ts'
42+
import { type TTemplateLiteralStatic } from '../engine/template-literal/index.ts'
4543
import { CreateRecord } from '../engine/record/record-create.ts'
4644

4745
// -------------------------------------------------------------------
4846
// Static
4947
// -------------------------------------------------------------------
48+
type StaticPropertyKey<Key extends string, Result extends PropertyKey = (
49+
Key extends TStringKey ? string :
50+
Key extends TIntegerKey ? number :
51+
Key extends TNumberKey ? number :
52+
Key extends `^${string}$` ? TTemplateLiteralStatic<Key> :
53+
string
54+
)> = Result
5055
export type StaticRecord<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Key extends string, Value extends TSchema,
56+
StaticKey extends PropertyKey = StaticPropertyKey<Key>,
5157
StaticValue extends unknown = StaticType<Stack, Direction, Context, This, Value>,
52-
Result extends Record<PropertyKey, unknown> = (
53-
Key extends TStringKey ? Record<string, StaticValue> :
54-
Key extends TIntegerKey ? Record<number, StaticValue> :
55-
Key extends TNumberKey ? Record<number, StaticValue> :
56-
Record<string, StaticValue>
57-
)
58-
> = Result
58+
> = Record<StaticKey, StaticValue>
5959
// -------------------------------------------------------------------
6060
// Keys
6161
// -------------------------------------------------------------------

tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Range } from './task/range/index.ts'
88
import { Metrics } from './task/metrics/index.ts'
99
import { Task } from 'tasksmith'
1010

11-
const Version = '1.0.69'
11+
const Version = '1.0.70'
1212

1313
// ------------------------------------------------------------------
1414
// Build
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Assert } from 'test'
2+
import { type XStatic } from 'typebox/schema'
3+
4+
// boolean true allows anything, so unknown
5+
Assert.IsExtendsMutual<
6+
XStatic<true>,
7+
unknown
8+
>(true)
9+
// boolean false disallows everything, so never
10+
Assert.IsExtendsMutual<
11+
XStatic<false>,
12+
never
13+
>(true)

test/typebox/static/type/record.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,53 @@ import { Assert } from 'test'
4343
const T = Type.Record(Type.TemplateLiteral("${'x' | 'y' | 'z'}${string}"), Type.Number())
4444
type T = Static<typeof T>
4545
Assert.IsExtendsMutual<T, {
46-
[x: string]: number
46+
[x: `x${string}`]: number
47+
[x: `y${string}`]: number
48+
[x: `z${string}`]: number
4749
}>(true)
4850
Assert.IsExtendsMutual<T, null>(false)
4951
}
52+
// ... with additional inference
53+
{
54+
const T = Type.Record(Type.TemplateLiteral('A${string}'), Type.Number())
55+
type T = Static<typeof T>
56+
Assert.IsExtendsMutual<T, {
57+
[x: `A${string}`]: number
58+
}>(true)
59+
}
60+
{
61+
const T = Type.Record(Type.TemplateLiteral('A${number}'), Type.Number())
62+
type T = Static<typeof T>
63+
Assert.IsExtendsMutual<T, {
64+
[x: `A${number}`]: number
65+
}>(true)
66+
}
67+
{
68+
const T = Type.Record(Type.TemplateLiteral('A${integer}'), Type.Number())
69+
type T = Static<typeof T>
70+
Assert.IsExtendsMutual<T, {
71+
[x: `A${number}`]: number
72+
}>(true)
73+
}
74+
{
75+
const T = Type.Record(Type.TemplateLiteral('A${boolean}'), Type.Number())
76+
type T = Static<typeof T>
77+
Assert.IsExtendsMutual<T, {
78+
Afalse: number
79+
Atrue: number
80+
}>(true)
81+
}
82+
{
83+
const T = Type.Record(Type.TemplateLiteral('A${true}'), Type.Number())
84+
type T = Static<typeof T>
85+
Assert.IsExtendsMutual<T, {
86+
Atrue: number
87+
}>(true)
88+
}
89+
{
90+
const T = Type.Record(Type.TemplateLiteral('A${false}'), Type.Number())
91+
type T = Static<typeof T>
92+
Assert.IsExtendsMutual<T, {
93+
Afalse: number
94+
}>(true)
95+
}

0 commit comments

Comments
 (0)