@sinclair/typebox 0.33.22 → 0.34.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/compiler/compiler.d.ts +2 -2
- package/build/cjs/compiler/compiler.js +13 -2
- package/build/cjs/errors/errors.js +7 -0
- package/build/cjs/index.d.ts +1 -2
- package/build/cjs/index.js +1 -2
- package/build/cjs/syntax/parse.d.ts +14 -15
- package/build/cjs/syntax/parse.js +6 -5
- package/build/cjs/syntax/runtime.d.ts +20 -4
- package/build/cjs/syntax/runtime.js +183 -28
- package/build/cjs/syntax/static.d.ts +107 -23
- package/build/cjs/type/array/array.d.ts +1 -1
- package/build/cjs/type/awaited/awaited.d.ts +8 -3
- package/build/cjs/type/awaited/awaited.js +20 -20
- package/build/cjs/type/computed/computed.d.ts +9 -0
- package/build/cjs/type/computed/computed.js +10 -0
- package/build/cjs/type/computed/index.d.ts +1 -0
- package/build/cjs/type/{deref → computed}/index.js +1 -1
- package/build/cjs/type/guard/kind.d.ts +12 -4
- package/build/cjs/type/guard/kind.js +15 -0
- package/build/cjs/type/guard/type.d.ts +9 -3
- package/build/cjs/type/guard/type.js +19 -0
- package/build/cjs/type/index.d.ts +1 -2
- package/build/cjs/type/index.js +1 -2
- package/build/cjs/type/indexed/indexed-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/indexed/indexed-from-mapped-key.js +10 -10
- package/build/cjs/type/indexed/indexed-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/indexed/indexed-from-mapped-result.js +11 -10
- package/build/cjs/type/indexed/indexed-property-keys.d.ts +6 -6
- package/build/cjs/type/indexed/indexed-property-keys.js +14 -14
- package/build/cjs/type/indexed/indexed.d.ts +34 -20
- package/build/cjs/type/indexed/indexed.js +59 -46
- package/build/cjs/type/intersect/intersect-evaluated.d.ts +6 -6
- package/build/cjs/type/intersect/intersect-evaluated.js +16 -16
- package/build/cjs/type/intersect/intersect.d.ts +2 -2
- package/build/cjs/type/intersect/intersect.js +6 -6
- package/build/cjs/type/keyof/keyof-from-mapped-result.d.ts +6 -6
- package/build/cjs/type/keyof/keyof-from-mapped-result.js +10 -10
- package/build/cjs/type/keyof/keyof-property-keys.d.ts +7 -7
- package/build/cjs/type/keyof/keyof-property-keys.js +22 -22
- package/build/cjs/type/keyof/keyof.d.ts +11 -6
- package/build/cjs/type/keyof/keyof.js +22 -13
- package/build/cjs/type/module/compute.d.ts +54 -0
- package/build/cjs/type/module/compute.js +154 -0
- package/build/cjs/type/module/index.d.ts +1 -0
- package/build/cjs/type/{strict → module}/index.js +1 -1
- package/build/cjs/type/module/infer.d.ts +43 -0
- package/build/cjs/type/module/infer.js +3 -0
- package/build/cjs/type/module/module.d.ts +27 -0
- package/build/cjs/type/module/module.js +37 -0
- package/build/cjs/type/not/not.d.ts +1 -1
- package/build/cjs/type/not/not.js +2 -2
- package/build/cjs/type/omit/omit-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/omit/omit-from-mapped-key.js +10 -12
- package/build/cjs/type/omit/omit-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/omit/omit-from-mapped-result.js +10 -10
- package/build/cjs/type/omit/omit.d.ts +26 -14
- package/build/cjs/type/omit/omit.js +46 -35
- package/build/cjs/type/partial/partial.d.ts +11 -7
- package/build/cjs/type/partial/partial.js +36 -24
- package/build/cjs/type/pick/pick-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/pick/pick-from-mapped-key.js +10 -10
- package/build/cjs/type/pick/pick-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/pick/pick-from-mapped-result.js +10 -10
- package/build/cjs/type/pick/pick.d.ts +25 -15
- package/build/cjs/type/pick/pick.js +47 -36
- package/build/cjs/type/record/record.d.ts +4 -2
- package/build/cjs/type/record/record.js +31 -28
- package/build/cjs/type/ref/ref.d.ts +4 -7
- package/build/cjs/type/ref/ref.js +3 -11
- package/build/cjs/type/required/required.d.ts +12 -8
- package/build/cjs/type/required/required.js +37 -25
- package/build/cjs/type/static/static.d.ts +1 -1
- package/build/cjs/type/tuple/tuple.d.ts +1 -1
- package/build/cjs/type/tuple/tuple.js +4 -4
- package/build/cjs/type/type/javascript.d.ts +10 -10
- package/build/cjs/type/type/javascript.js +6 -6
- package/build/cjs/type/type/json.d.ts +45 -68
- package/build/cjs/type/type/json.js +82 -98
- package/build/cjs/type/type/type.d.ts +1 -2
- package/build/cjs/type/type/type.js +55 -57
- package/build/cjs/type/union/union-evaluated.d.ts +6 -6
- package/build/cjs/type/union/union-evaluated.js +11 -10
- package/build/cjs/type/union/union.d.ts +1 -1
- package/build/cjs/type/union/union.js +4 -4
- package/build/cjs/value/cast/cast.js +8 -1
- package/build/cjs/value/check/check.js +11 -4
- package/build/cjs/value/clean/clean.js +8 -1
- package/build/cjs/value/convert/convert.js +7 -0
- package/build/cjs/value/create/create.js +7 -0
- package/build/cjs/value/default/default.js +7 -0
- package/build/cjs/value/transform/decode.js +18 -7
- package/build/cjs/value/transform/encode.js +18 -7
- package/build/cjs/value/transform/has.js +18 -18
- package/build/esm/compiler/compiler.d.mts +2 -2
- package/build/esm/compiler/compiler.mjs +13 -2
- package/build/esm/errors/errors.mjs +7 -0
- package/build/esm/index.d.mts +1 -2
- package/build/esm/index.mjs +1 -2
- package/build/esm/syntax/parse.d.mts +14 -15
- package/build/esm/syntax/parse.mjs +6 -5
- package/build/esm/syntax/runtime.d.mts +20 -4
- package/build/esm/syntax/runtime.mjs +183 -28
- package/build/esm/syntax/static.d.mts +107 -23
- package/build/esm/type/array/array.d.mts +1 -1
- package/build/esm/type/awaited/awaited.d.mts +8 -3
- package/build/esm/type/awaited/awaited.mjs +20 -20
- package/build/esm/type/computed/computed.d.mts +9 -0
- package/build/esm/type/computed/computed.mjs +6 -0
- package/build/esm/type/computed/index.d.mts +1 -0
- package/build/esm/type/computed/index.mjs +1 -0
- package/build/esm/type/guard/kind.d.mts +12 -4
- package/build/esm/type/guard/kind.mjs +12 -0
- package/build/esm/type/guard/type.d.mts +9 -3
- package/build/esm/type/guard/type.mjs +17 -0
- package/build/esm/type/index.d.mts +1 -2
- package/build/esm/type/index.mjs +1 -2
- package/build/esm/type/indexed/indexed-from-mapped-key.d.mts +6 -6
- package/build/esm/type/indexed/indexed-from-mapped-key.mjs +10 -10
- package/build/esm/type/indexed/indexed-from-mapped-result.d.mts +5 -5
- package/build/esm/type/indexed/indexed-from-mapped-result.mjs +11 -10
- package/build/esm/type/indexed/indexed-property-keys.d.mts +6 -6
- package/build/esm/type/indexed/indexed-property-keys.mjs +14 -14
- package/build/esm/type/indexed/indexed.d.mts +34 -20
- package/build/esm/type/indexed/indexed.mjs +55 -42
- package/build/esm/type/intersect/intersect-evaluated.d.mts +6 -6
- package/build/esm/type/intersect/intersect-evaluated.mjs +16 -16
- package/build/esm/type/intersect/intersect.d.mts +2 -2
- package/build/esm/type/intersect/intersect.mjs +6 -6
- package/build/esm/type/keyof/keyof-from-mapped-result.d.mts +6 -6
- package/build/esm/type/keyof/keyof-from-mapped-result.mjs +10 -10
- package/build/esm/type/keyof/keyof-property-keys.d.mts +7 -7
- package/build/esm/type/keyof/keyof-property-keys.mjs +22 -22
- package/build/esm/type/keyof/keyof.d.mts +11 -6
- package/build/esm/type/keyof/keyof.mjs +22 -13
- package/build/esm/type/module/compute.d.mts +54 -0
- package/build/esm/type/module/compute.mjs +148 -0
- package/build/esm/type/module/index.d.mts +1 -0
- package/build/esm/type/module/index.mjs +1 -0
- package/build/esm/type/module/infer.d.mts +43 -0
- package/build/esm/type/module/infer.mjs +1 -0
- package/build/esm/type/module/module.d.mts +27 -0
- package/build/esm/type/module/module.mjs +31 -0
- package/build/esm/type/not/not.d.mts +1 -1
- package/build/esm/type/not/not.mjs +2 -2
- package/build/esm/type/omit/omit-from-mapped-key.d.mts +6 -6
- package/build/esm/type/omit/omit-from-mapped-key.mjs +10 -12
- package/build/esm/type/omit/omit-from-mapped-result.d.mts +5 -5
- package/build/esm/type/omit/omit-from-mapped-result.mjs +10 -10
- package/build/esm/type/omit/omit.d.mts +26 -14
- package/build/esm/type/omit/omit.mjs +43 -32
- package/build/esm/type/partial/partial.d.mts +11 -7
- package/build/esm/type/partial/partial.mjs +29 -17
- package/build/esm/type/pick/pick-from-mapped-key.d.mts +6 -6
- package/build/esm/type/pick/pick-from-mapped-key.mjs +10 -10
- package/build/esm/type/pick/pick-from-mapped-result.d.mts +5 -5
- package/build/esm/type/pick/pick-from-mapped-result.mjs +10 -10
- package/build/esm/type/pick/pick.d.mts +25 -15
- package/build/esm/type/pick/pick.mjs +43 -32
- package/build/esm/type/record/record.d.mts +4 -2
- package/build/esm/type/record/record.mjs +15 -12
- package/build/esm/type/ref/ref.d.mts +4 -7
- package/build/esm/type/ref/ref.mjs +3 -11
- package/build/esm/type/required/required.d.mts +12 -8
- package/build/esm/type/required/required.mjs +33 -21
- package/build/esm/type/static/static.d.mts +1 -1
- package/build/esm/type/tuple/tuple.d.mts +1 -1
- package/build/esm/type/tuple/tuple.mjs +4 -4
- package/build/esm/type/type/javascript.d.mts +10 -10
- package/build/esm/type/type/javascript.mjs +6 -6
- package/build/esm/type/type/json.d.mts +45 -68
- package/build/esm/type/type/json.mjs +47 -63
- package/build/esm/type/type/type.d.mts +1 -2
- package/build/esm/type/type/type.mjs +1 -2
- package/build/esm/type/union/union-evaluated.d.mts +6 -6
- package/build/esm/type/union/union-evaluated.mjs +11 -10
- package/build/esm/type/union/union.d.mts +1 -1
- package/build/esm/type/union/union.mjs +4 -4
- package/build/esm/value/cast/cast.mjs +9 -2
- package/build/esm/value/check/check.mjs +11 -4
- package/build/esm/value/clean/clean.mjs +9 -2
- package/build/esm/value/convert/convert.mjs +7 -0
- package/build/esm/value/create/create.mjs +7 -0
- package/build/esm/value/default/default.mjs +7 -0
- package/build/esm/value/transform/decode.mjs +13 -2
- package/build/esm/value/transform/encode.mjs +13 -2
- package/build/esm/value/transform/has.mjs +2 -2
- package/package.json +1 -1
- package/readme.md +230 -97
- package/build/cjs/type/deref/deref.d.ts +0 -22
- package/build/cjs/type/deref/deref.js +0 -106
- package/build/cjs/type/deref/index.d.ts +0 -1
- package/build/cjs/type/strict/index.d.ts +0 -1
- package/build/cjs/type/strict/strict.d.ts +0 -12
- package/build/cjs/type/strict/strict.js +0 -16
- package/build/esm/type/deref/deref.d.mts +0 -22
- package/build/esm/type/deref/deref.mjs +0 -102
- package/build/esm/type/deref/index.d.mts +0 -1
- package/build/esm/type/deref/index.mjs +0 -1
- package/build/esm/type/strict/index.d.mts +0 -1
- package/build/esm/type/strict/index.mjs +0 -1
- package/build/esm/type/strict/strict.d.mts +0 -12
- package/build/esm/type/strict/strict.mjs +0 -12
|
@@ -2,19 +2,20 @@ import { MappedResult } from '../mapped/index.mjs';
|
|
|
2
2
|
import { IndexPropertyKeys } from './indexed-property-keys.mjs';
|
|
3
3
|
import { Index } from './index.mjs';
|
|
4
4
|
// prettier-ignore
|
|
5
|
-
function FromProperties(
|
|
6
|
-
const
|
|
7
|
-
for (const K2 of Object.getOwnPropertyNames(
|
|
8
|
-
|
|
5
|
+
function FromProperties(type, properties, options) {
|
|
6
|
+
const result = {};
|
|
7
|
+
for (const K2 of Object.getOwnPropertyNames(properties)) {
|
|
8
|
+
const keys = IndexPropertyKeys(properties[K2]);
|
|
9
|
+
result[K2] = Index(type, keys, options);
|
|
9
10
|
}
|
|
10
|
-
return
|
|
11
|
+
return result;
|
|
11
12
|
}
|
|
12
13
|
// prettier-ignore
|
|
13
|
-
function FromMappedResult(
|
|
14
|
-
return FromProperties(
|
|
14
|
+
function FromMappedResult(type, mappedResult, options) {
|
|
15
|
+
return FromProperties(type, mappedResult.properties, options);
|
|
15
16
|
}
|
|
16
17
|
// prettier-ignore
|
|
17
|
-
export function IndexFromMappedResult(
|
|
18
|
-
const
|
|
19
|
-
return MappedResult(
|
|
18
|
+
export function IndexFromMappedResult(type, mappedResult, options) {
|
|
19
|
+
const properties = FromMappedResult(type, mappedResult, options);
|
|
20
|
+
return MappedResult(properties);
|
|
20
21
|
}
|
|
@@ -4,11 +4,11 @@ import type { TInteger } from '../integer/index.mjs';
|
|
|
4
4
|
import type { TNumber } from '../number/index.mjs';
|
|
5
5
|
import type { TSchema } from '../schema/index.mjs';
|
|
6
6
|
import type { TUnion } from '../union/index.mjs';
|
|
7
|
-
type TFromTemplateLiteral<
|
|
8
|
-
type TFromUnion<
|
|
9
|
-
type TFromLiteral<
|
|
10
|
-
export type TIndexPropertyKeys<
|
|
11
|
-
]);
|
|
7
|
+
type TFromTemplateLiteral<TemplateLiteral extends TTemplateLiteral, Result extends string[] = TTemplateLiteralGenerate<TemplateLiteral>> = Result;
|
|
8
|
+
type TFromUnion<Types extends TSchema[], Result extends string[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromUnion<Right, [...Result, ...TIndexPropertyKeys<Left>]> : Result);
|
|
9
|
+
type TFromLiteral<LiteralValue extends TLiteralValue> = (LiteralValue extends PropertyKey ? [`${LiteralValue}`] : []);
|
|
10
|
+
export type TIndexPropertyKeys<Type extends TSchema, Result extends PropertyKey[] = (Type extends TTemplateLiteral ? TFromTemplateLiteral<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> : Type extends TLiteral<infer LiteralValue extends TLiteralValue> ? TFromLiteral<LiteralValue> : Type extends TNumber ? ['[number]'] : Type extends TInteger ? ['[number]'] : [
|
|
11
|
+
])> = Result;
|
|
12
12
|
/** Returns a tuple of PropertyKeys derived from the given TSchema */
|
|
13
|
-
export declare function IndexPropertyKeys<
|
|
13
|
+
export declare function IndexPropertyKeys<Type extends TSchema>(type: Type): TIndexPropertyKeys<Type>;
|
|
14
14
|
export {};
|
|
@@ -4,16 +4,16 @@ import { TemplateLiteralGenerate } from '../template-literal/index.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/kind.mjs';
|
|
6
6
|
// prettier-ignore
|
|
7
|
-
function FromTemplateLiteral(
|
|
8
|
-
const
|
|
9
|
-
return
|
|
7
|
+
function FromTemplateLiteral(templateLiteral) {
|
|
8
|
+
const result = TemplateLiteralGenerate(templateLiteral);
|
|
9
|
+
return result.map(S => S.toString());
|
|
10
10
|
}
|
|
11
11
|
// prettier-ignore
|
|
12
|
-
function FromUnion(
|
|
13
|
-
const
|
|
14
|
-
for (const
|
|
15
|
-
|
|
16
|
-
return
|
|
12
|
+
function FromUnion(type) {
|
|
13
|
+
const result = [];
|
|
14
|
+
for (const left of type)
|
|
15
|
+
result.push(...IndexPropertyKeys(left));
|
|
16
|
+
return result;
|
|
17
17
|
}
|
|
18
18
|
// prettier-ignore
|
|
19
19
|
function FromLiteral(T) {
|
|
@@ -22,11 +22,11 @@ function FromLiteral(T) {
|
|
|
22
22
|
}
|
|
23
23
|
/** Returns a tuple of PropertyKeys derived from the given TSchema */
|
|
24
24
|
// prettier-ignore
|
|
25
|
-
export function IndexPropertyKeys(
|
|
26
|
-
return [...new Set((IsTemplateLiteral(
|
|
27
|
-
IsUnion(
|
|
28
|
-
IsLiteral(
|
|
29
|
-
IsNumber(
|
|
30
|
-
IsInteger(
|
|
25
|
+
export function IndexPropertyKeys(type) {
|
|
26
|
+
return [...new Set((IsTemplateLiteral(type) ? FromTemplateLiteral(type) :
|
|
27
|
+
IsUnion(type) ? FromUnion(type.anyOf) :
|
|
28
|
+
IsLiteral(type) ? FromLiteral(type.const) :
|
|
29
|
+
IsNumber(type) ? ['[number]'] :
|
|
30
|
+
IsInteger(type) ? ['[number]'] :
|
|
31
31
|
[]))];
|
|
32
32
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
|
+
import { type TComputed } from '../computed/index.mjs';
|
|
3
|
+
import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
|
|
2
4
|
import { type TObject, type TProperties } from '../object/index.mjs';
|
|
3
5
|
import { type Assert } from '../helpers/index.mjs';
|
|
4
6
|
import { type TNever } from '../never/index.mjs';
|
|
@@ -8,33 +10,45 @@ import { TMappedResult, type TMappedKey } from '../mapped/index.mjs';
|
|
|
8
10
|
import { type TUnion } from '../union/index.mjs';
|
|
9
11
|
import { type TTuple } from '../tuple/index.mjs';
|
|
10
12
|
import { type TArray } from '../array/index.mjs';
|
|
13
|
+
import { type TRef } from '../ref/index.mjs';
|
|
11
14
|
import { type TIntersectEvaluated } from '../intersect/index.mjs';
|
|
12
15
|
import { type TUnionEvaluated } from '../union/index.mjs';
|
|
13
16
|
import { type TIndexPropertyKeys } from './indexed-property-keys.mjs';
|
|
14
17
|
import { type TIndexFromMappedKey } from './indexed-from-mapped-key.mjs';
|
|
15
18
|
import { type TIndexFromMappedResult } from './indexed-from-mapped-result.mjs';
|
|
16
|
-
type TFromRest<T extends TSchema[], K extends PropertyKey,
|
|
17
|
-
type TFromIntersectRest<
|
|
18
|
-
type TFromIntersect<
|
|
19
|
-
type TFromUnionRest<
|
|
20
|
-
type TFromUnion<
|
|
21
|
-
type TFromTuple<
|
|
22
|
-
type TFromArray<
|
|
23
|
-
type AssertPropertyKey<T> = Assert<T, string | number>;
|
|
24
|
-
type TFromProperty<
|
|
25
|
-
export type TIndexFromPropertyKey<
|
|
26
|
-
export declare function IndexFromPropertyKey<
|
|
27
|
-
export type TIndexFromPropertyKeys<
|
|
28
|
-
export declare function IndexFromPropertyKeys<
|
|
29
|
-
type
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
type TFromRest<T extends TSchema[], K extends PropertyKey, Result extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Result, Assert<TIndexFromPropertyKey<L, K>, TSchema>]> : Result);
|
|
20
|
+
type TFromIntersectRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TNever ? TFromIntersectRest<Right, [...Result]> : TFromIntersectRest<Right, [...Result, Left]> : Result);
|
|
21
|
+
type TFromIntersect<Types extends TSchema[], Key extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<Types, Key>>>);
|
|
22
|
+
type TFromUnionRest<Types extends TSchema[], Result extends TSchema[] = []> = Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TNever ? [] : TFromUnionRest<Right, [Left, ...Result]> : Result;
|
|
23
|
+
type TFromUnion<Types extends TSchema[], Key extends PropertyKey> = (TUnionEvaluated<TFromUnionRest<TFromRest<Types, Key>>>);
|
|
24
|
+
type TFromTuple<Types extends TSchema[], Key extends PropertyKey, Result extends TSchema = (Key extends '[number]' ? TUnionEvaluated<Types> : Key extends keyof Types ? Types[Key] extends infer Type extends TSchema ? Type : TNever : TNever)> = Result;
|
|
25
|
+
type TFromArray<Type extends TSchema, Key extends PropertyKey> = (Key extends '[number]' ? Type : TNever);
|
|
26
|
+
type AssertPropertyKey<T extends unknown> = Assert<T, string | number>;
|
|
27
|
+
type TFromProperty<Properties extends TProperties, Key extends PropertyKey, Result extends TSchema = (Key extends keyof Properties ? Properties[Key] : `${AssertPropertyKey<Key>}` extends `${AssertPropertyKey<keyof Properties>}` ? Properties[AssertPropertyKey<Key>] : TNever)> = Result;
|
|
28
|
+
export type TIndexFromPropertyKey<Type extends TSchema, Key extends PropertyKey> = (Type extends TRecursive<infer S extends TSchema> ? TIndexFromPropertyKey<S, Key> : Type extends TIntersect<infer S extends TSchema[]> ? TFromIntersect<S, Key> : Type extends TUnion<infer S extends TSchema[]> ? TFromUnion<S, Key> : Type extends TTuple<infer S extends TSchema[]> ? TFromTuple<S, Key> : Type extends TArray<infer S extends TSchema> ? TFromArray<S, Key> : Type extends TObject<infer S extends TProperties> ? TFromProperty<S, Key> : TNever);
|
|
29
|
+
export declare function IndexFromPropertyKey<Type extends TSchema, Key extends PropertyKey>(type: Type, key: Key): TIndexFromPropertyKey<Type, Key>;
|
|
30
|
+
export type TIndexFromPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = (PropertyKeys extends [infer Left extends PropertyKey, ...infer Right extends PropertyKey[]] ? TIndexFromPropertyKeys<Type, Right, [...Result, Assert<TIndexFromPropertyKey<Type, Left>, TSchema>]> : Result);
|
|
31
|
+
export declare function IndexFromPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[]>(type: Type, propertyKeys: [...PropertyKeys]): TIndexFromPropertyKeys<Type, PropertyKeys>;
|
|
32
|
+
type TFromType<Type extends TSchema, PropertyKeys extends PropertyKey[], Result extends TSchema[] = TIndexFromPropertyKeys<Type, PropertyKeys>> = TUnionEvaluated<Result>;
|
|
33
|
+
type TUnionFromPropertyKeys<PropertyKeys extends PropertyKey[], Result extends TLiteral[] = []> = (PropertyKeys extends [infer Key extends PropertyKey, ...infer Rest extends PropertyKey[]] ? Key extends TLiteralValue ? TUnionFromPropertyKeys<Rest, [...Result, TLiteral<Key>]> : TUnionFromPropertyKeys<Rest, [...Result]> : TUnionEvaluated<Result>);
|
|
34
|
+
type TResolvePropertyKeys<Key extends TSchema | PropertyKey[]> = Key extends TSchema ? TIndexPropertyKeys<Key> : Key;
|
|
35
|
+
type TResolveTypeKey<Key extends TSchema | PropertyKey[]> = Key extends PropertyKey[] ? TUnionFromPropertyKeys<Key> : Key;
|
|
36
|
+
export type TIndex<Type extends TSchema, Key extends TSchema | PropertyKey[], IsTypeRef extends boolean = Type extends TRef ? true : false, IsKeyRef extends boolean = Key extends TRef ? true : false> = (Key extends TMappedResult ? TIndexFromMappedResult<Type, Key> : Key extends TMappedKey ? TIndexFromMappedKey<Type, Key> : [
|
|
37
|
+
IsTypeRef,
|
|
38
|
+
IsKeyRef
|
|
39
|
+
] extends [true, true] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : [
|
|
40
|
+
IsTypeRef,
|
|
41
|
+
IsKeyRef
|
|
42
|
+
] extends [false, true] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : [
|
|
43
|
+
IsTypeRef,
|
|
44
|
+
IsKeyRef
|
|
45
|
+
] extends [true, false] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : TFromType<Type, TResolvePropertyKeys<Key>>);
|
|
32
46
|
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
33
|
-
export declare function Index<
|
|
47
|
+
export declare function Index<Type extends TSchema, Key extends PropertyKey[]>(type: Type, key: readonly [...Key], options?: SchemaOptions): TIndex<Type, Key>;
|
|
34
48
|
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
35
|
-
export declare function Index<
|
|
49
|
+
export declare function Index<Type extends TSchema, Key extends TMappedKey>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
|
|
36
50
|
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
37
|
-
export declare function Index<
|
|
51
|
+
export declare function Index<Type extends TSchema, Key extends TMappedResult>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
|
|
38
52
|
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
39
|
-
export declare function Index<
|
|
53
|
+
export declare function Index<Type extends TSchema, Key extends TSchema>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
|
|
40
54
|
export {};
|
|
@@ -1,78 +1,91 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
|
+
import { Computed } from '../computed/index.mjs';
|
|
3
|
+
import { Literal } from '../literal/index.mjs';
|
|
2
4
|
import { Never } from '../never/index.mjs';
|
|
3
5
|
import { IntersectEvaluated } from '../intersect/index.mjs';
|
|
4
6
|
import { UnionEvaluated } from '../union/index.mjs';
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// Infrastructure
|
|
9
|
+
// ------------------------------------------------------------------
|
|
5
10
|
import { IndexPropertyKeys } from './indexed-property-keys.mjs';
|
|
6
11
|
import { IndexFromMappedKey } from './indexed-from-mapped-key.mjs';
|
|
7
12
|
import { IndexFromMappedResult } from './indexed-from-mapped-result.mjs';
|
|
8
13
|
// ------------------------------------------------------------------
|
|
9
|
-
//
|
|
14
|
+
// KindGuard
|
|
10
15
|
// ------------------------------------------------------------------
|
|
11
|
-
import { IsArray, IsIntersect, IsObject, IsMappedKey, IsMappedResult, IsNever, IsSchema, IsTuple, IsUnion } from '../guard/kind.mjs';
|
|
16
|
+
import { IsArray, IsIntersect, IsObject, IsMappedKey, IsMappedResult, IsNever, IsSchema, IsTuple, IsUnion, IsLiteralValue, IsRef } from '../guard/kind.mjs';
|
|
17
|
+
import { IsArray as IsArrayValue } from '../guard/value.mjs';
|
|
12
18
|
// prettier-ignore
|
|
13
|
-
function FromRest(
|
|
14
|
-
return
|
|
19
|
+
function FromRest(types, key) {
|
|
20
|
+
return types.map(left => IndexFromPropertyKey(left, key));
|
|
15
21
|
}
|
|
16
22
|
// prettier-ignore
|
|
17
|
-
function FromIntersectRest(
|
|
18
|
-
return
|
|
23
|
+
function FromIntersectRest(types) {
|
|
24
|
+
return types.filter(left => !IsNever(left));
|
|
19
25
|
}
|
|
20
26
|
// prettier-ignore
|
|
21
|
-
function FromIntersect(
|
|
22
|
-
return (IntersectEvaluated(FromIntersectRest(FromRest(
|
|
27
|
+
function FromIntersect(types, key) {
|
|
28
|
+
return (IntersectEvaluated(FromIntersectRest(FromRest(types, key))));
|
|
23
29
|
}
|
|
24
30
|
// prettier-ignore
|
|
25
|
-
function FromUnionRest(
|
|
26
|
-
return (
|
|
31
|
+
function FromUnionRest(types) {
|
|
32
|
+
return (types.some(L => IsNever(L))
|
|
27
33
|
? []
|
|
28
|
-
:
|
|
34
|
+
: types);
|
|
29
35
|
}
|
|
30
36
|
// prettier-ignore
|
|
31
|
-
function FromUnion(
|
|
32
|
-
return (UnionEvaluated(FromUnionRest(FromRest(
|
|
37
|
+
function FromUnion(types, key) {
|
|
38
|
+
return (UnionEvaluated(FromUnionRest(FromRest(types, key))));
|
|
33
39
|
}
|
|
34
40
|
// prettier-ignore
|
|
35
|
-
function FromTuple(
|
|
36
|
-
return (
|
|
37
|
-
|
|
41
|
+
function FromTuple(types, key) {
|
|
42
|
+
return (key === '[number]' ? UnionEvaluated(types) :
|
|
43
|
+
key in types ? types[key] :
|
|
38
44
|
Never());
|
|
39
45
|
}
|
|
40
46
|
// prettier-ignore
|
|
41
|
-
function FromArray(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
: Never());
|
|
47
|
+
function FromArray(type, key) {
|
|
48
|
+
// ... ?
|
|
49
|
+
return (key === '[number]' ? type : Never());
|
|
45
50
|
}
|
|
46
51
|
// prettier-ignore
|
|
47
|
-
function FromProperty(
|
|
48
|
-
return (
|
|
52
|
+
function FromProperty(properties, key) {
|
|
53
|
+
return (key in properties ? properties[key] : Never());
|
|
49
54
|
}
|
|
50
55
|
// prettier-ignore
|
|
51
|
-
export function IndexFromPropertyKey(
|
|
52
|
-
return (IsIntersect(
|
|
53
|
-
IsUnion(
|
|
54
|
-
IsTuple(
|
|
55
|
-
IsArray(
|
|
56
|
-
IsObject(
|
|
56
|
+
export function IndexFromPropertyKey(type, key) {
|
|
57
|
+
return (IsIntersect(type) ? FromIntersect(type.allOf, key) :
|
|
58
|
+
IsUnion(type) ? FromUnion(type.anyOf, key) :
|
|
59
|
+
IsTuple(type) ? FromTuple(type.items ?? [], key) :
|
|
60
|
+
IsArray(type) ? FromArray(type.items, key) :
|
|
61
|
+
IsObject(type) ? FromProperty(type.properties, key) :
|
|
57
62
|
Never());
|
|
58
63
|
}
|
|
59
64
|
// prettier-ignore
|
|
60
|
-
export function IndexFromPropertyKeys(
|
|
61
|
-
return
|
|
65
|
+
export function IndexFromPropertyKeys(type, propertyKeys) {
|
|
66
|
+
return propertyKeys.map(left => IndexFromPropertyKey(type, left));
|
|
62
67
|
}
|
|
63
68
|
// prettier-ignore
|
|
64
|
-
function
|
|
65
|
-
|
|
69
|
+
function FromType(type, propertyKeys) {
|
|
70
|
+
const result = IndexFromPropertyKeys(type, propertyKeys);
|
|
71
|
+
return UnionEvaluated(result);
|
|
72
|
+
}
|
|
73
|
+
// prettier-ignore
|
|
74
|
+
function UnionFromPropertyKeys(propertyKeys) {
|
|
75
|
+
const result = propertyKeys.reduce((result, key) => IsLiteralValue(key) ? [...result, Literal(key)] : result, []);
|
|
76
|
+
return UnionEvaluated(result);
|
|
66
77
|
}
|
|
67
78
|
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
// prettier-ignore
|
|
80
|
+
export function Index(type, key, options) {
|
|
81
|
+
const typeKey = IsArrayValue(key) ? UnionFromPropertyKeys(key) : key;
|
|
82
|
+
const propertyKeys = IsSchema(key) ? IndexPropertyKeys(key) : key;
|
|
83
|
+
const isTypeRef = IsRef(type);
|
|
84
|
+
const isKeyRef = IsRef(key);
|
|
85
|
+
return (IsMappedResult(key) ? IndexFromMappedResult(type, key, options) :
|
|
86
|
+
IsMappedKey(key) ? IndexFromMappedKey(type, key, options) :
|
|
87
|
+
(isTypeRef && isKeyRef) ? Computed('Index', [type, typeKey], options) :
|
|
88
|
+
(!isTypeRef && isKeyRef) ? Computed('Index', [type, typeKey], options) :
|
|
89
|
+
(isTypeRef && !isKeyRef) ? Computed('Index', [type, typeKey], options) :
|
|
90
|
+
CreateType(FromType(type, propertyKeys), options));
|
|
78
91
|
}
|
|
@@ -3,11 +3,11 @@ import { type TNever } from '../never/index.mjs';
|
|
|
3
3
|
import { type TOptional } from '../optional/index.mjs';
|
|
4
4
|
import type { TReadonly } from '../readonly/index.mjs';
|
|
5
5
|
import { TIntersect, IntersectOptions } from './intersect-type.mjs';
|
|
6
|
-
type TIsIntersectOptional<
|
|
7
|
-
type TRemoveOptionalFromType<
|
|
8
|
-
type TRemoveOptionalFromRest<
|
|
9
|
-
type TResolveIntersect<
|
|
10
|
-
export type TIntersectEvaluated<
|
|
6
|
+
type TIsIntersectOptional<Types extends TSchema[]> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TOptional<TSchema> ? TIsIntersectOptional<Right> : false : true);
|
|
7
|
+
type TRemoveOptionalFromType<Type extends TSchema> = (Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TRemoveOptionalFromType<Type>> : Type extends TOptional<infer Type extends TSchema> ? TRemoveOptionalFromType<Type> : Type);
|
|
8
|
+
type TRemoveOptionalFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TOptional<infer Type extends TSchema> ? TRemoveOptionalFromRest<Right, [...Result, TRemoveOptionalFromType<Type>]> : TRemoveOptionalFromRest<Right, [...Result, Left]> : Result);
|
|
9
|
+
type TResolveIntersect<Types extends TSchema[]> = (TIsIntersectOptional<Types> extends true ? TOptional<TIntersect<TRemoveOptionalFromRest<Types>>> : TIntersect<TRemoveOptionalFromRest<Types>>);
|
|
10
|
+
export type TIntersectEvaluated<Types extends TSchema[]> = (Types extends [TSchema] ? Types[0] : Types extends [] ? TNever : TResolveIntersect<Types>);
|
|
11
11
|
/** `[Json]` Creates an evaluated Intersect type */
|
|
12
|
-
export declare function IntersectEvaluated<
|
|
12
|
+
export declare function IntersectEvaluated<Types extends TSchema[], Result extends TSchema = TIntersectEvaluated<Types>>(types: [...Types], options?: IntersectOptions): Result;
|
|
13
13
|
export {};
|
|
@@ -9,30 +9,30 @@ import { IntersectCreate } from './intersect-create.mjs';
|
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
import { IsOptional, IsTransform } from '../guard/kind.mjs';
|
|
11
11
|
// prettier-ignore
|
|
12
|
-
function IsIntersectOptional(
|
|
13
|
-
return
|
|
12
|
+
function IsIntersectOptional(types) {
|
|
13
|
+
return types.every(left => IsOptional(left));
|
|
14
14
|
}
|
|
15
15
|
// prettier-ignore
|
|
16
|
-
function RemoveOptionalFromType(
|
|
17
|
-
return (Discard(
|
|
16
|
+
function RemoveOptionalFromType(type) {
|
|
17
|
+
return (Discard(type, [OptionalKind]));
|
|
18
18
|
}
|
|
19
19
|
// prettier-ignore
|
|
20
|
-
function RemoveOptionalFromRest(
|
|
21
|
-
return
|
|
20
|
+
function RemoveOptionalFromRest(types) {
|
|
21
|
+
return types.map(left => IsOptional(left) ? RemoveOptionalFromType(left) : left);
|
|
22
22
|
}
|
|
23
23
|
// prettier-ignore
|
|
24
|
-
function ResolveIntersect(
|
|
25
|
-
return (IsIntersectOptional(
|
|
26
|
-
? Optional(IntersectCreate(RemoveOptionalFromRest(
|
|
27
|
-
: IntersectCreate(RemoveOptionalFromRest(
|
|
24
|
+
function ResolveIntersect(types, options) {
|
|
25
|
+
return (IsIntersectOptional(types)
|
|
26
|
+
? Optional(IntersectCreate(RemoveOptionalFromRest(types), options))
|
|
27
|
+
: IntersectCreate(RemoveOptionalFromRest(types), options));
|
|
28
28
|
}
|
|
29
29
|
/** `[Json]` Creates an evaluated Intersect type */
|
|
30
|
-
export function IntersectEvaluated(
|
|
31
|
-
if (
|
|
30
|
+
export function IntersectEvaluated(types, options = {}) {
|
|
31
|
+
if (types.length === 1)
|
|
32
|
+
return CreateType(types[0], options);
|
|
33
|
+
if (types.length === 0)
|
|
32
34
|
return Never(options);
|
|
33
|
-
if (
|
|
34
|
-
return CreateType(T[0], options);
|
|
35
|
-
if (T.some((schema) => IsTransform(schema)))
|
|
35
|
+
if (types.some((schema) => IsTransform(schema)))
|
|
36
36
|
throw new Error('Cannot intersect transform types');
|
|
37
|
-
return ResolveIntersect(
|
|
37
|
+
return ResolveIntersect(types, options);
|
|
38
38
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { TSchema } from '../schema/index.mjs';
|
|
2
2
|
import { type TNever } from '../never/index.mjs';
|
|
3
3
|
import { TIntersect, IntersectOptions } from './intersect-type.mjs';
|
|
4
|
-
export type Intersect<
|
|
4
|
+
export type Intersect<Types extends TSchema[]> = (Types extends [TSchema] ? Types[0] : Types extends [] ? TNever : TIntersect<Types>);
|
|
5
5
|
/** `[Json]` Creates an evaluated Intersect type */
|
|
6
|
-
export declare function Intersect<
|
|
6
|
+
export declare function Intersect<Types extends TSchema[]>(types: [...Types], options?: IntersectOptions): Intersect<Types>;
|
|
@@ -6,12 +6,12 @@ import { IntersectCreate } from './intersect-create.mjs';
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
import { IsTransform } from '../guard/kind.mjs';
|
|
8
8
|
/** `[Json]` Creates an evaluated Intersect type */
|
|
9
|
-
export function Intersect(
|
|
10
|
-
if (
|
|
9
|
+
export function Intersect(types, options) {
|
|
10
|
+
if (types.length === 1)
|
|
11
|
+
return CreateType(types[0], options);
|
|
12
|
+
if (types.length === 0)
|
|
11
13
|
return Never(options);
|
|
12
|
-
if (
|
|
13
|
-
return CreateType(T[0], options);
|
|
14
|
-
if (T.some((schema) => IsTransform(schema)))
|
|
14
|
+
if (types.some((schema) => IsTransform(schema)))
|
|
15
15
|
throw new Error('Cannot intersect transform types');
|
|
16
|
-
return IntersectCreate(
|
|
16
|
+
return IntersectCreate(types, options);
|
|
17
17
|
}
|
|
@@ -2,11 +2,11 @@ import type { SchemaOptions } from '../schema/index.mjs';
|
|
|
2
2
|
import type { Ensure, Evaluate } from '../helpers/index.mjs';
|
|
3
3
|
import type { TProperties } from '../object/index.mjs';
|
|
4
4
|
import { type TMappedResult } from '../mapped/index.mjs';
|
|
5
|
-
import { type
|
|
6
|
-
type TFromProperties<
|
|
7
|
-
[K2 in keyof
|
|
5
|
+
import { type TKeyOfFromType } from './keyof.mjs';
|
|
6
|
+
type TFromProperties<Properties extends TProperties> = ({
|
|
7
|
+
[K2 in keyof Properties]: TKeyOfFromType<Properties[K2]>;
|
|
8
8
|
});
|
|
9
|
-
type TFromMappedResult<
|
|
10
|
-
export type TKeyOfFromMappedResult<
|
|
11
|
-
export declare function KeyOfFromMappedResult<
|
|
9
|
+
type TFromMappedResult<MappedResult extends TMappedResult> = (Evaluate<TFromProperties<MappedResult['properties']>>);
|
|
10
|
+
export type TKeyOfFromMappedResult<MappedResult extends TMappedResult, Properties extends TProperties = TFromMappedResult<MappedResult>> = (Ensure<TMappedResult<Properties>>);
|
|
11
|
+
export declare function KeyOfFromMappedResult<MappedResult extends TMappedResult, Properties extends TProperties = TFromMappedResult<MappedResult>>(mappedResult: MappedResult, options?: SchemaOptions): TMappedResult<Properties>;
|
|
12
12
|
export {};
|
|
@@ -2,18 +2,18 @@ import { MappedResult } from '../mapped/index.mjs';
|
|
|
2
2
|
import { KeyOf } from './keyof.mjs';
|
|
3
3
|
import { Clone } from '../clone/value.mjs';
|
|
4
4
|
// prettier-ignore
|
|
5
|
-
function FromProperties(
|
|
6
|
-
const
|
|
7
|
-
for (const K2 of globalThis.Object.getOwnPropertyNames(
|
|
8
|
-
|
|
9
|
-
return
|
|
5
|
+
function FromProperties(properties, options) {
|
|
6
|
+
const result = {};
|
|
7
|
+
for (const K2 of globalThis.Object.getOwnPropertyNames(properties))
|
|
8
|
+
result[K2] = KeyOf(properties[K2], Clone(options));
|
|
9
|
+
return result;
|
|
10
10
|
}
|
|
11
11
|
// prettier-ignore
|
|
12
|
-
function FromMappedResult(
|
|
13
|
-
return FromProperties(
|
|
12
|
+
function FromMappedResult(mappedResult, options) {
|
|
13
|
+
return FromProperties(mappedResult.properties, options);
|
|
14
14
|
}
|
|
15
15
|
// prettier-ignore
|
|
16
|
-
export function KeyOfFromMappedResult(
|
|
17
|
-
const
|
|
18
|
-
return MappedResult(
|
|
16
|
+
export function KeyOfFromMappedResult(mappedResult, options) {
|
|
17
|
+
const properties = FromMappedResult(mappedResult, options);
|
|
18
|
+
return MappedResult(properties);
|
|
19
19
|
}
|
|
@@ -7,18 +7,18 @@ import type { TTuple } from '../tuple/index.mjs';
|
|
|
7
7
|
import type { TArray } from '../array/index.mjs';
|
|
8
8
|
import type { TObject, TProperties } from '../object/index.mjs';
|
|
9
9
|
import { type TSetUnionMany, type TSetIntersectMany } from '../sets/index.mjs';
|
|
10
|
-
type TFromRest<
|
|
11
|
-
type TFromIntersect<
|
|
12
|
-
type TFromUnion<
|
|
13
|
-
type TFromTuple<
|
|
10
|
+
type TFromRest<Types extends TSchema[], Result extends PropertyKey[][] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TKeyOfPropertyKeys<L>]> : Result);
|
|
11
|
+
type TFromIntersect<Types extends TSchema[], PropertyKeysArray extends PropertyKey[][] = TFromRest<Types>, PropertyKeys extends PropertyKey[] = TSetUnionMany<PropertyKeysArray>> = PropertyKeys;
|
|
12
|
+
type TFromUnion<Types extends TSchema[], PropertyKeysArray extends PropertyKey[][] = TFromRest<Types>, PropertyKeys extends PropertyKey[] = TSetIntersectMany<PropertyKeysArray>> = PropertyKeys;
|
|
13
|
+
type TFromTuple<Types extends TSchema[], Indexer extends string = ZeroString, Acc extends PropertyKey[] = []> = Types extends [infer _ extends TSchema, ...infer R extends TSchema[]] ? TFromTuple<R, TIncrement<Indexer>, [...Acc, Indexer]> : Acc;
|
|
14
14
|
type TFromArray<_ extends TSchema> = ([
|
|
15
15
|
'[number]'
|
|
16
16
|
]);
|
|
17
|
-
type TFromProperties<
|
|
18
|
-
export type TKeyOfPropertyKeys<
|
|
17
|
+
type TFromProperties<Properties extends TProperties> = (UnionToTuple<keyof Properties>);
|
|
18
|
+
export type TKeyOfPropertyKeys<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TKeyOfPropertyKeys<Type> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<Types> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<Types> : Type extends TArray<infer Type extends TSchema> ? TFromArray<Type> : Type extends TObject<infer Properties extends TProperties> ? TFromProperties<Properties> : [
|
|
19
19
|
]);
|
|
20
20
|
/** Returns a tuple of PropertyKeys derived from the given TSchema. */
|
|
21
|
-
export declare function KeyOfPropertyKeys<
|
|
21
|
+
export declare function KeyOfPropertyKeys<Type extends TSchema>(type: Type): TKeyOfPropertyKeys<Type>;
|
|
22
22
|
/** Returns a regular expression pattern derived from the given TSchema */
|
|
23
23
|
export declare function KeyOfPattern(schema: TSchema): string;
|
|
24
24
|
export {};
|
|
@@ -4,27 +4,27 @@ import { SetUnionMany, SetIntersectMany } from '../sets/index.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
import { IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsRecord } from '../guard/kind.mjs';
|
|
6
6
|
// prettier-ignore
|
|
7
|
-
function FromRest(
|
|
8
|
-
const
|
|
9
|
-
for (const L of
|
|
10
|
-
|
|
11
|
-
return
|
|
7
|
+
function FromRest(types) {
|
|
8
|
+
const result = [];
|
|
9
|
+
for (const L of types)
|
|
10
|
+
result.push(KeyOfPropertyKeys(L));
|
|
11
|
+
return result;
|
|
12
12
|
}
|
|
13
13
|
// prettier-ignore
|
|
14
|
-
function FromIntersect(
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
return
|
|
14
|
+
function FromIntersect(types) {
|
|
15
|
+
const propertyKeysArray = FromRest(types);
|
|
16
|
+
const propertyKeys = SetUnionMany(propertyKeysArray);
|
|
17
|
+
return propertyKeys;
|
|
18
18
|
}
|
|
19
19
|
// prettier-ignore
|
|
20
|
-
function FromUnion(
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
return
|
|
20
|
+
function FromUnion(types) {
|
|
21
|
+
const propertyKeysArray = FromRest(types);
|
|
22
|
+
const propertyKeys = SetIntersectMany(propertyKeysArray);
|
|
23
|
+
return propertyKeys;
|
|
24
24
|
}
|
|
25
25
|
// prettier-ignore
|
|
26
|
-
function FromTuple(
|
|
27
|
-
return
|
|
26
|
+
function FromTuple(types) {
|
|
27
|
+
return types.map((_, indexer) => indexer.toString());
|
|
28
28
|
}
|
|
29
29
|
// prettier-ignore
|
|
30
30
|
function FromArray(_) {
|
|
@@ -50,13 +50,13 @@ function FromPatternProperties(patternProperties) {
|
|
|
50
50
|
}
|
|
51
51
|
/** Returns a tuple of PropertyKeys derived from the given TSchema. */
|
|
52
52
|
// prettier-ignore
|
|
53
|
-
export function KeyOfPropertyKeys(
|
|
54
|
-
return (IsIntersect(
|
|
55
|
-
IsUnion(
|
|
56
|
-
IsTuple(
|
|
57
|
-
IsArray(
|
|
58
|
-
IsObject(
|
|
59
|
-
IsRecord(
|
|
53
|
+
export function KeyOfPropertyKeys(type) {
|
|
54
|
+
return (IsIntersect(type) ? FromIntersect(type.allOf) :
|
|
55
|
+
IsUnion(type) ? FromUnion(type.anyOf) :
|
|
56
|
+
IsTuple(type) ? FromTuple(type.items ?? []) :
|
|
57
|
+
IsArray(type) ? FromArray(type.items) :
|
|
58
|
+
IsObject(type) ? FromProperties(type.properties) :
|
|
59
|
+
IsRecord(type) ? FromPatternProperties(type.patternProperties) :
|
|
60
60
|
[]);
|
|
61
61
|
}
|
|
62
62
|
// ----------------------------------------------------------------
|
|
@@ -4,13 +4,18 @@ import type { TMappedResult } from '../mapped/index.mjs';
|
|
|
4
4
|
import type { SchemaOptions } from '../schema/index.mjs';
|
|
5
5
|
import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
|
|
6
6
|
import { type TNumber } from '../number/index.mjs';
|
|
7
|
+
import { TComputed } from '../computed/index.mjs';
|
|
8
|
+
import { type TRef } from '../ref/index.mjs';
|
|
7
9
|
import { type TKeyOfPropertyKeys } from './keyof-property-keys.mjs';
|
|
8
10
|
import { type TUnionEvaluated } from '../union/index.mjs';
|
|
9
11
|
import { type TKeyOfFromMappedResult } from './keyof-from-mapped-result.mjs';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'KeyOf', [TComputed<Target, Parameters>]>>;
|
|
13
|
+
type TFromRef<Ref extends string> = Ensure<TComputed<'KeyOf', [TRef<Ref>]>>;
|
|
14
|
+
/** `[Internal]` Used by KeyOfFromMappedResult */
|
|
15
|
+
export type TKeyOfFromType<Type extends TSchema, PropertyKeys extends PropertyKey[] = TKeyOfPropertyKeys<Type>, PropertyKeyTypes extends TSchema[] = TKeyOfPropertyKeysToRest<PropertyKeys>, Result = TUnionEvaluated<PropertyKeyTypes>> = Ensure<Result>;
|
|
16
|
+
export type TKeyOfPropertyKeysToRest<PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = (PropertyKeys extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? L extends '[number]' ? TKeyOfPropertyKeysToRest<R, [...Result, TNumber]> : TKeyOfPropertyKeysToRest<R, [...Result, TLiteral<Assert<L, TLiteralValue>>]> : Result);
|
|
17
|
+
export declare function KeyOfPropertyKeysToRest<PropertyKeys extends PropertyKey[]>(propertyKeys: [...PropertyKeys]): TKeyOfPropertyKeysToRest<PropertyKeys>;
|
|
18
|
+
export type TKeyOf<Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : Type extends TRef<infer Ref extends string> ? TFromRef<Ref> : Type extends TMappedResult ? TKeyOfFromMappedResult<Type> : TKeyOfFromType<Type>);
|
|
13
19
|
/** `[Json]` Creates a KeyOf type */
|
|
14
|
-
export declare function KeyOf<
|
|
15
|
-
|
|
16
|
-
export declare function KeyOf<T extends TSchema>(T: T, options?: SchemaOptions): TKeyOf<T>;
|
|
20
|
+
export declare function KeyOf<Type extends TSchema>(type: Type, options?: SchemaOptions): TKeyOf<Type>;
|
|
21
|
+
export {};
|