Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
---

### Version Updates
- [Revision 1.0.70](https://github.com/sinclairzx81/typebox/pull/1498)
- Static Boolean Schematics | Static Template Literal Record Key
- [Revision 1.0.69](https://github.com/sinclairzx81/typebox/pull/1493)
- Optimize Union Evaluation for Distinct Sets
- [Revision 1.0.68](https://github.com/sinclairzx81/typebox/pull/1492)
Expand Down
20 changes: 17 additions & 3 deletions src/schema/static/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,24 @@ type TKeywordsEvaluated<Schema extends unknown,
: Schema
> = Result
// ------------------------------------------------------------------
// XStatic
// XStaticObject
// ------------------------------------------------------------------
export type XStaticSchema<Stack extends string[], Root extends XSchema, Schema extends XSchema,
export type XStaticObject<Stack extends string[], Root extends XSchema, Schema extends XSchema,
Keywords extends unknown[] = TFromKeywords<Stack, Root, Schema>,
Intersected extends unknown = TKeywordsIntersected<Keywords>,
Evaluated extends unknown = TKeywordsEvaluated<Intersected>
> = Evaluated
> = Evaluated
// ------------------------------------------------------------------
// XStaticBoolean
// ------------------------------------------------------------------
export type XStaticBoolean<Schema extends boolean,
Result extends unknown = Schema extends false ? never : unknown
> = Result
// ------------------------------------------------------------------
// XStaticSchema
// ------------------------------------------------------------------
export type XStaticSchema<Stack extends string[], Root extends XSchema, Schema extends XSchema,
Result extends unknown = Schema extends boolean
? XStaticBoolean<Schema>
: XStaticObject<Stack, Root, Schema>
> = Result
22 changes: 11 additions & 11 deletions src/type/types/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ import { Memory } from '../../system/memory/index.ts'
import { Guard } from '../../guard/index.ts'
import { type TSchema, type TObjectOptions, IsKind } from './schema.ts'
import { type StaticType, type StaticDirection } from './static.ts'

import { type TProperties } from './properties.ts'
import { type TInteger, Integer, IntegerPattern } from './integer.ts'

import { type TTemplateLiteral } from './template-literal.ts'
import { type TNumber, Number, NumberPattern } from './number.ts'
import { type TString, String, StringPattern } from './string.ts'

import { type TDeferred, Deferred } from './deferred.ts'

import { type TInstantiate, Instantiate } from '../engine/instantiate.ts'
import { type TTemplateLiteralStatic } from '../engine/template-literal/index.ts'
import { CreateRecord } from '../engine/record/record-create.ts'

// -------------------------------------------------------------------
// Static
// -------------------------------------------------------------------
type StaticPropertyKey<Key extends string, Result extends PropertyKey = (
Key extends TStringKey ? string :
Key extends TIntegerKey ? number :
Key extends TNumberKey ? number :
Key extends `^${string}$` ? TTemplateLiteralStatic<Key> :
string
)> = Result
export type StaticRecord<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Key extends string, Value extends TSchema,
StaticKey extends PropertyKey = StaticPropertyKey<Key>,
StaticValue extends unknown = StaticType<Stack, Direction, Context, This, Value>,
Result extends Record<PropertyKey, unknown> = (
Key extends TStringKey ? Record<string, StaticValue> :
Key extends TIntegerKey ? Record<number, StaticValue> :
Key extends TNumberKey ? Record<number, StaticValue> :
Record<string, StaticValue>
)
> = Result
> = Record<StaticKey, StaticValue>
// -------------------------------------------------------------------
// Keys
// -------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Range } from './task/range/index.ts'
import { Metrics } from './task/metrics/index.ts'
import { Task } from 'tasksmith'

const Version = '1.0.69'
const Version = '1.0.70'

// ------------------------------------------------------------------
// Build
Expand Down
13 changes: 13 additions & 0 deletions test/typebox/static/schema/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Assert } from 'test'
import { type XStatic } from 'typebox/schema'

// boolean true allows anything, so unknown
Assert.IsExtendsMutual<
XStatic<true>,
unknown
>(true)
// boolean false disallows everything, so never
Assert.IsExtendsMutual<
XStatic<false>,
never
>(true)
48 changes: 47 additions & 1 deletion test/typebox/static/type/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,53 @@ import { Assert } from 'test'
const T = Type.Record(Type.TemplateLiteral("${'x' | 'y' | 'z'}${string}"), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
[x: string]: number
[x: `x${string}`]: number
[x: `y${string}`]: number
[x: `z${string}`]: number
}>(true)
Assert.IsExtendsMutual<T, null>(false)
}
// ... with additional inference
{
const T = Type.Record(Type.TemplateLiteral('A${string}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
[x: `A${string}`]: number
}>(true)
}
{
const T = Type.Record(Type.TemplateLiteral('A${number}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
[x: `A${number}`]: number
}>(true)
}
{
const T = Type.Record(Type.TemplateLiteral('A${integer}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
[x: `A${number}`]: number
}>(true)
}
{
const T = Type.Record(Type.TemplateLiteral('A${boolean}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
Afalse: number
Atrue: number
}>(true)
}
{
const T = Type.Record(Type.TemplateLiteral('A${true}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
Atrue: number
}>(true)
}
{
const T = Type.Record(Type.TemplateLiteral('A${false}'), Type.Number())
type T = Static<typeof T>
Assert.IsExtendsMutual<T, {
Afalse: number
}>(true)
}