Skip to content

Commit

Permalink
Revision 0.32.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 30, 2023
1 parent 532eba7 commit 5ca162e
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 32 deletions.
38 changes: 11 additions & 27 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,24 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'
import { Type, Static, TIndex, TNumber, TObject, MappedKey, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

const T = Type.Object({
const S = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})

type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)
const T = Type.Mapped(Type.KeyOf(S), (K) => K)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------
type TIndexMappedResult<T extends TSchema, K extends PropertyKey[]> = K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? [[L, TIndex<T, [L]>], ...TIndexMappedResult<T, R>] : []

const C = TypeCompiler.Compile(T)
type TIndexMapped<T extends TSchema, K extends TSchema> = K extends MappedKey<infer K extends PropertyKey[]> ? TIndexMappedResult<T, K> : []

console.log(C.Code())
type K = MappedKey<['x', 'y']>

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------
type T = TObject<{
x: TNumber
y: TNumber
}>

console.log(C.Check(V))
type R = TIndexMapped<T, K>
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ type S = Static<typeof T> // type S = 'A' | 'B' | 'C'
```
<a name='types-guard'></a>
### Type Guard
### TypeGuard
TypeBox can type check its own types with the TypeGuard module. This module is written for reflection and provides structural tests for every TypeBox type. Functions of this module return `is` guards which can be used with TypeScript control flow assertions to obtain schema inference. The following guards that the value A is TString.
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ export { Extends, type TExtends } from './type/extends/index'
export { Extract, type TExtract } from './type/extract/index'
export { Function, type TFunction } from './type/function/index'
export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index'
export { Index, type TIndex } from './type/indexed/index'
export { Index, type TIndex, IndexedKeyResolve, IndexedTypeResolve } from './type/indexed/index'
export { InstanceType, type TInstanceType } from './type/instance-type/index'
export { Integer, type TInteger, type IntegerOptions } from './type/integer/index'
export { Intersect, type TIntersect, type IntersectOptions } from './type/intersect/index'
export { Iterator, type TIterator } from './type/iterator/index'
export { KeyOf, type TKeyOf } from './type/keyof/index'
export { KeyOf, type TKeyOf, KeyOfStringResolve, KeyOfStringResolvePattern, KeyOfTypeResolve } from './type/keyof/index'
export { Literal, type TLiteral } from './type/literal/index'
export { Lowercase, type TLowercase } from './type/intrinsic/index'
export { Mapped, type MappedKey, type MappedFunction, type MappedResult } from './type/mapped/index'
export { Never, type TNever } from './type/never/index'
export { Not, type TNot } from './type/not/index'
export { Null, type TNull } from './type/null/index'
Expand Down
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export * from './intrinsic/index'
export * from './iterator/index'
export * from './keyof/index'
export * from './literal/index'
export * from './mapped/index'
export * from './modifiers/index'
export * from './never/index'
export * from './not/index'
Expand Down
45 changes: 45 additions & 0 deletions src/type/indexed/indexed-mapped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TSchema } from '../schema/index'
import { TIndex } from './indexed'
import { MappedKey } from '../mapped/index'

// prettier-ignore
type TIndexMappedResult<T extends TSchema, K extends PropertyKey[]> =
K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]]
? [[L, TIndex<T, [L]>], ...TIndexMappedResult<T, R>]
: []

// prettier-ignore
export type TIndexMapped<
T extends TSchema,
K extends TSchema
> = K extends MappedKey<infer K extends PropertyKey[]>
? TIndexMappedResult<T, K>
: []
29 changes: 29 additions & 0 deletions src/type/mapped/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './mapped'
92 changes: 92 additions & 0 deletions src/type/mapped/mapped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema } from '../schema/index'
import type { Ensure, Evaluate } from '../helpers/index'
import { Object, type TObject, type TProperties } from '../object/index'
import { Literal } from '../literal/index'
import { IndexedKeyResolve } from '../indexed/indexed-key'
import { TSchema as IsSchemaType } from '../guard/type'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// MappedKey
// ------------------------------------------------------------------
// prettier-ignore
export interface MappedKey<T extends PropertyKey[] = []> extends TSchema {
[Kind]: 'MappedKey'
static: T[number]
anyOf: []
}
// ------------------------------------------------------------------
// MappedFunction
// ------------------------------------------------------------------
// prettier-ignore
export type MappedFunction<K extends PropertyKey[]> = (T: MappedKey<K>) => TSchema
// ------------------------------------------------------------------
// MappedResult
// ------------------------------------------------------------------
// prettier-ignore
export type MappedResultRest<K extends PropertyKey[], T extends TSchema> =
K extends [infer L extends string, ...infer R extends PropertyKey[]]
? {[_ in `${L}`]: T} & MappedResultRest<R, T>
: {}
// prettier-ignore
export type MappedResult<
K extends PropertyKey[],
F extends MappedFunction<K>,
R extends TProperties = Evaluate<MappedResultRest<K, ReturnType<F>>>
> = (
Ensure<TObject<R>>
)
// ------------------------------------------------------------------
// Mapped
// ------------------------------------------------------------------
// prettier-ignore
export function Mapped<
K extends TSchema,
I extends PropertyKey[] = IndexedKeyResolve<K>,
F extends MappedFunction<I> = MappedFunction<I>,
R extends MappedResult<I, F> = MappedResult<I, F>
>(key: K, map: F): R
// prettier-ignore
export function Mapped<
K extends PropertyKey[],
F extends MappedFunction<K> = MappedFunction<K>,
R extends MappedResult<K, F> = MappedResult<K, F>
>(key: [...K], map: F): R
// prettier-ignore
export function Mapped(key: any, map: Function) {
const keys = IsSchemaType(key) ? IndexedKeyResolve(key) : key as PropertyKey[]
const anyOf = keys.map(key => Literal(key as string))
const mapped = { [Kind]: 'MappedKey', anyOf }
const properties = keys.reduce((acc, key) => {
return { ...acc, [key]: map(mapped) }
}, {} as TProperties)
return Object(properties)
}
20 changes: 20 additions & 0 deletions src/type/type/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { Intersect, type IntersectOptions, type IntersectResolve } from '../inte
import { Capitalize, Uncapitalize, Lowercase, Uppercase, type TCapitalize, type TUncapitalize, type TLowercase, type TUppercase } from '../intrinsic/index'
import { KeyOf, type TKeyOf } from '../keyof/index'
import { Literal, type TLiteral, type TLiteralValue } from '../literal/index'
import { Mapped, type MappedFunction, type MappedResult } from '../mapped/index'
import { Never, type TNever } from '../never/index'
import { Not, type TNot } from '../not/index'
import { Null, type TNull } from '../null/index'
Expand Down Expand Up @@ -167,6 +168,25 @@ export class JsonTypeBuilder {
public Lowercase<T extends TSchema>(schema: T, options: SchemaOptions = {}): TLowercase<T> {
return Lowercase(schema, options)
}
/** [Json] Creates a Mapped type */
// prettier-ignore
public Mapped<
K extends TSchema,
I extends PropertyKey[] = IndexedKeyResolve<K>,
F extends MappedFunction<I> = MappedFunction<I>,
R extends MappedResult<I, F> = MappedResult<I, F>
>(key: K, map: F): R
/** [Json] Creates a Mapped type */
// prettier-ignore
public Mapped<
K extends PropertyKey[],
F extends MappedFunction<K> = MappedFunction<K>,
R extends MappedResult<K, F> = MappedResult<K, F>
>(key: [...K], map: F): R
/** [Json] Creates a Mapped type */
public Mapped(key: any, map: Function) {
return Mapped(key, map as any)
}
/** `[Json]` Creates a Never type */
public Never(options: SchemaOptions = {}): TNever {
return Never(options)
Expand Down
1 change: 1 addition & 0 deletions src/type/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export { Capitalize, Uncapitalize, Lowercase, Uppercase } from '../intrinsic/ind
export { Iterator } from '../iterator/index'
export { KeyOf } from '../keyof/index'
export { Literal } from '../literal/index'
export { Mapped } from '../mapped/index'
export { Never } from '../never/index'
export { Not } from '../not/index'
export { Null } from '../null/index'
Expand Down

0 comments on commit 5ca162e

Please sign in to comment.