@sinclair/typebox 0.32.0-dev-19 → 0.32.0-dev-21

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.
Files changed (98) hide show
  1. package/build/import/compiler/compiler.mjs +62 -62
  2. package/build/import/errors/errors.mjs +62 -62
  3. package/build/import/index.d.mts +6 -6
  4. package/build/import/index.mjs +3 -3
  5. package/build/import/type/indexed/indexed-property-keys.d.mts +2 -2
  6. package/build/import/type/indexed/indexed-property-keys.mjs +6 -11
  7. package/build/import/type/intrinsic/intrinsic.mjs +3 -3
  8. package/build/import/type/optional/index.d.mts +1 -0
  9. package/build/import/type/optional/index.mjs +1 -0
  10. package/build/import/type/optional/optional-from-mapped-result.d.mts +12 -0
  11. package/build/import/type/optional/optional-from-mapped-result.mjs +17 -0
  12. package/build/import/type/optional/optional.d.mts +19 -2
  13. package/build/import/type/optional/optional.mjs +18 -2
  14. package/build/import/type/readonly/index.d.mts +1 -0
  15. package/build/import/type/readonly/index.mjs +1 -0
  16. package/build/import/type/readonly/readonly-from-mapped-result.d.mts +12 -0
  17. package/build/import/type/readonly/readonly-from-mapped-result.mjs +17 -0
  18. package/build/import/type/readonly/readonly.d.mts +18 -1
  19. package/build/import/type/readonly/readonly.mjs +18 -2
  20. package/build/import/type/record/record.d.mts +2 -2
  21. package/build/import/type/record/record.mjs +2 -3
  22. package/build/import/type/schema/schema.d.mts +1 -1
  23. package/build/import/type/symbol/symbol.d.mts +1 -1
  24. package/build/import/type/template-literal/finite.d.mts +7 -5
  25. package/build/import/type/template-literal/finite.mjs +14 -5
  26. package/build/import/type/template-literal/generate.d.mts +14 -8
  27. package/build/import/type/template-literal/generate.mjs +19 -9
  28. package/build/import/type/template-literal/index.d.mts +1 -1
  29. package/build/import/type/template-literal/index.mjs +1 -1
  30. package/build/import/type/template-literal/{parser.mjs → parse.mjs} +7 -2
  31. package/build/import/type/template-literal/syntax.d.mts +1 -1
  32. package/build/import/type/template-literal/syntax.mjs +0 -1
  33. package/build/import/type/template-literal/template-literal.d.mts +2 -2
  34. package/build/import/type/template-literal/template-literal.mjs +1 -1
  35. package/build/import/type/template-literal/union.d.mts +2 -2
  36. package/build/import/type/template-literal/union.mjs +5 -10
  37. package/build/import/type/type/json.d.mts +19 -7
  38. package/build/import/type/type/json.mjs +5 -5
  39. package/build/import/value/cast/cast.mjs +20 -20
  40. package/build/import/value/check/check.mjs +62 -62
  41. package/build/import/value/clean/clean.mjs +16 -16
  42. package/build/import/value/convert/convert.mjs +36 -36
  43. package/build/import/value/create/create.mjs +66 -67
  44. package/build/import/value/default/default.mjs +16 -16
  45. package/build/import/value/transform/decode.mjs +18 -18
  46. package/build/import/value/transform/encode.mjs +18 -18
  47. package/build/import/value/transform/has.mjs +28 -28
  48. package/build/require/compiler/compiler.js +62 -62
  49. package/build/require/errors/errors.js +62 -62
  50. package/build/require/index.d.ts +6 -6
  51. package/build/require/index.js +6 -2
  52. package/build/require/type/indexed/indexed-property-keys.d.ts +2 -2
  53. package/build/require/type/indexed/indexed-property-keys.js +4 -9
  54. package/build/require/type/intrinsic/intrinsic.js +2 -2
  55. package/build/require/type/optional/index.d.ts +1 -0
  56. package/build/require/type/optional/index.js +1 -0
  57. package/build/require/type/optional/optional-from-mapped-result.d.ts +12 -0
  58. package/build/require/type/optional/optional-from-mapped-result.js +22 -0
  59. package/build/require/type/optional/optional.d.ts +19 -2
  60. package/build/require/type/optional/optional.js +18 -2
  61. package/build/require/type/readonly/index.d.ts +1 -0
  62. package/build/require/type/readonly/index.js +1 -0
  63. package/build/require/type/readonly/readonly-from-mapped-result.d.ts +12 -0
  64. package/build/require/type/readonly/readonly-from-mapped-result.js +22 -0
  65. package/build/require/type/readonly/readonly.d.ts +18 -1
  66. package/build/require/type/readonly/readonly.js +18 -2
  67. package/build/require/type/record/record.d.ts +2 -2
  68. package/build/require/type/record/record.js +1 -2
  69. package/build/require/type/schema/schema.d.ts +1 -1
  70. package/build/require/type/symbol/symbol.d.ts +1 -1
  71. package/build/require/type/template-literal/finite.d.ts +7 -5
  72. package/build/require/type/template-literal/finite.js +16 -6
  73. package/build/require/type/template-literal/generate.d.ts +14 -8
  74. package/build/require/type/template-literal/generate.js +22 -11
  75. package/build/require/type/template-literal/index.d.ts +1 -1
  76. package/build/require/type/template-literal/index.js +1 -1
  77. package/build/require/type/template-literal/{parser.js → parse.js} +7 -2
  78. package/build/require/type/template-literal/syntax.d.ts +1 -1
  79. package/build/require/type/template-literal/syntax.js +0 -1
  80. package/build/require/type/template-literal/template-literal.d.ts +2 -2
  81. package/build/require/type/template-literal/template-literal.js +1 -1
  82. package/build/require/type/template-literal/union.d.ts +2 -2
  83. package/build/require/type/template-literal/union.js +5 -10
  84. package/build/require/type/type/json.d.ts +19 -7
  85. package/build/require/type/type/json.js +5 -5
  86. package/build/require/value/cast/cast.js +20 -20
  87. package/build/require/value/check/check.js +62 -62
  88. package/build/require/value/clean/clean.js +16 -16
  89. package/build/require/value/convert/convert.js +36 -36
  90. package/build/require/value/create/create.js +65 -66
  91. package/build/require/value/default/default.js +16 -16
  92. package/build/require/value/transform/decode.js +18 -18
  93. package/build/require/value/transform/encode.js +18 -18
  94. package/build/require/value/transform/has.js +28 -28
  95. package/package.json +1 -1
  96. package/readme.md +52 -52
  97. /package/build/import/type/template-literal/{parser.d.mts → parse.d.mts} +0 -0
  98. /package/build/require/type/template-literal/{parser.d.ts → parse.d.ts} +0 -0
@@ -42,12 +42,12 @@ export { Null } from './type/null/index.mjs';
42
42
  export { Number } from './type/number/index.mjs';
43
43
  export { Object } from './type/object/index.mjs';
44
44
  export { Omit } from './type/omit/index.mjs';
45
- export { Optional } from './type/optional/index.mjs';
45
+ export { Optional, OptionalFromMappedResult } from './type/optional/index.mjs';
46
46
  export { Parameters } from './type/parameters/index.mjs';
47
47
  export { Partial, PartialFromMappedResult } from './type/partial/index.mjs';
48
48
  export { Pick } from './type/pick/index.mjs';
49
49
  export { Promise } from './type/promise/index.mjs';
50
- export { Readonly } from './type/readonly/index.mjs';
50
+ export { Readonly, ReadonlyFromMappedResult } from './type/readonly/index.mjs';
51
51
  export { ReadonlyOptional } from './type/readonly-optional/index.mjs';
52
52
  export { Record } from './type/record/index.mjs';
53
53
  export { Recursive } from './type/recursive/index.mjs';
@@ -59,7 +59,7 @@ export { ReturnType } from './type/return-type/index.mjs';
59
59
  export { Strict } from './type/strict/index.mjs';
60
60
  export { String } from './type/string/index.mjs';
61
61
  export { Symbol } from './type/symbol/index.mjs';
62
- export { TemplateLiteral, IsTemplateLiteralFinite, TemplateLiteralParse, TemplateLiteralParseExact, TemplateLiteralGenerate } from './type/template-literal/index.mjs';
62
+ export { TemplateLiteral, IsTemplateLiteralFinite, IsTemplateLiteralExpressionFinite, TemplateLiteralParse, TemplateLiteralParseExact, TemplateLiteralGenerate, TemplateLiteralExpressionGenerate, } from './type/template-literal/index.mjs';
63
63
  export { Transform, TransformDecodeBuilder, TransformEncodeBuilder } from './type/transform/index.mjs';
64
64
  export { Tuple } from './type/tuple/index.mjs';
65
65
  export { Uint8Array } from './type/uint8array/index.mjs';
@@ -1,10 +1,10 @@
1
- import { type TTemplateLiteral, TemplateLiteralGenerate, IsTemplateLiteralFinite } from '../template-literal/index.mjs';
1
+ import { type TTemplateLiteral, TTemplateLiteralGenerate } from '../template-literal/index.mjs';
2
2
  import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
3
3
  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 FromTemplateLiteral<T extends TTemplateLiteral, F = IsTemplateLiteralFinite<T>> = (F extends true ? TemplateLiteralGenerate<T> extends infer R extends string[] ? R : [] : []);
7
+ type FromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemplateLiteralGenerate<T>> = (R);
8
8
  declare function FromTemplateLiteral<T extends TTemplateLiteral>(T: T): FromTemplateLiteral<T>;
9
9
  type FromUnion<T extends TSchema[]> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [...TIndexPropertyKeys<L>, ...FromUnion<R>] : []);
10
10
  declare function FromUnion<T extends TSchema[]>(T: T): FromUnion<T>;
@@ -1,19 +1,13 @@
1
- import { TemplateLiteralParseExact, IsTemplateLiteralFinite, TemplateLiteralToUnion } from '../template-literal/index.mjs';
1
+ import { TemplateLiteralGenerate } from '../template-literal/index.mjs';
2
2
  // ------------------------------------------------------------------
3
3
  // TypeGuard
4
4
  // ------------------------------------------------------------------
5
5
  // prettier-ignore
6
- import { IsTemplateLiteral, IsUnionLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/type.mjs';
6
+ import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/type.mjs';
7
7
  // prettier-ignore
8
8
  function FromTemplateLiteral(T) {
9
- const E = TemplateLiteralParseExact(T.pattern);
10
- const F = IsTemplateLiteralFinite(E);
11
- const S = TemplateLiteralToUnion(T);
12
- return (F === true
13
- ? IsUnionLiteral(S)
14
- ? S.anyOf.map(S => S.const.toString())
15
- : []
16
- : []);
9
+ const R = TemplateLiteralGenerate(T);
10
+ return R.map(S => S.toString());
17
11
  }
18
12
  // prettier-ignore
19
13
  function FromUnion(T) {
@@ -24,7 +18,8 @@ function FromUnion(T) {
24
18
  }
25
19
  // prettier-ignore
26
20
  function FromLiteral(T) {
27
- return ([T.toString()]);
21
+ return ([T.toString()] // TS 5.4 observes TLiteralValue as not having a toString()
22
+ );
28
23
  }
29
24
  /** Returns a tuple of PropertyKeys derived from the given TSchema */
30
25
  // prettier-ignore
@@ -1,4 +1,4 @@
1
- import { TemplateLiteral, TemplateLiteralParseExact, IsTemplateLiteralFinite, TemplateLiteralGenerate } from '../template-literal/index.mjs';
1
+ import { TemplateLiteral, TemplateLiteralParseExact, IsTemplateLiteralExpressionFinite, TemplateLiteralExpressionGenerate } from '../template-literal/index.mjs';
2
2
  import { IntrinsicFromMappedKey } from './intrinsic-from-mapped-key.mjs';
3
3
  import { Literal } from '../literal/index.mjs';
4
4
  import { Union } from '../union/index.mjs';
@@ -28,10 +28,10 @@ function FromTemplateLiteral(schema, mode, options) {
28
28
  // note: template literals require special runtime handling as they are encoded in string patterns.
29
29
  // This diverges from the mapped type which would otherwise map on the template literal kind.
30
30
  const expression = TemplateLiteralParseExact(schema.pattern);
31
- const finite = IsTemplateLiteralFinite(expression);
31
+ const finite = IsTemplateLiteralExpressionFinite(expression);
32
32
  if (!finite)
33
33
  return { ...schema, pattern: FromLiteralValue(schema.pattern, mode) };
34
- const strings = [...TemplateLiteralGenerate(expression)];
34
+ const strings = [...TemplateLiteralExpressionGenerate(expression)];
35
35
  const literals = strings.map((value) => Literal(value));
36
36
  const mapped = FromRest(literals, mode);
37
37
  const union = Union(mapped);
@@ -1 +1,2 @@
1
+ export * from './optional-from-mapped-result.mjs';
1
2
  export * from './optional.mjs';
@@ -1 +1,2 @@
1
+ export * from './optional-from-mapped-result.mjs';
1
2
  export * from './optional.mjs';
@@ -0,0 +1,12 @@
1
+ import type { TProperties } from '../object/index.mjs';
2
+ import { type TMappedResult } from '../mapped/index.mjs';
3
+ import { type TOptionalWithFlag } from './optional.mjs';
4
+ type FromProperties<K extends TProperties, F extends boolean> = ({
5
+ [K2 in keyof K]: TOptionalWithFlag<K[K2], F>;
6
+ });
7
+ declare function FromProperties<K extends TProperties, F extends boolean>(K: K, F: F): FromProperties<K, F>;
8
+ type FromMappedResult<K extends TMappedResult, F extends boolean> = (FromProperties<K['properties'], F>);
9
+ declare function FromMappedResult<K extends TMappedResult, F extends boolean>(K: K, F: F): FromMappedResult<K, F>;
10
+ export type TOptionalFromMappedResult<T extends TMappedResult, F extends boolean, P extends TProperties = FromMappedResult<T, F>> = (TMappedResult<P>);
11
+ export declare function OptionalFromMappedResult<T extends TMappedResult, F extends boolean, P extends TProperties = FromMappedResult<T, F>>(T: T, F: F): TMappedResult<P>;
12
+ export {};
@@ -0,0 +1,17 @@
1
+ import { MappedResult } from '../mapped/index.mjs';
2
+ import { Optional } from './optional.mjs';
3
+ // prettier-ignore
4
+ function FromProperties(K, F) {
5
+ return globalThis.Object.getOwnPropertyNames(K).reduce((Acc, K2) => {
6
+ return { ...Acc, [K2]: Optional(K[K2], F) };
7
+ }, {});
8
+ }
9
+ // prettier-ignore
10
+ function FromMappedResult(K, F) {
11
+ return FromProperties(K.properties, F);
12
+ }
13
+ // prettier-ignore
14
+ export function OptionalFromMappedResult(T, F) {
15
+ const P = FromMappedResult(T, F);
16
+ return MappedResult(P);
17
+ }
@@ -1,7 +1,24 @@
1
1
  import type { TSchema } from '../schema/index.mjs';
2
+ import type { Ensure } from '../helpers/index.mjs';
2
3
  import { OptionalKind } from '../symbols/index.mjs';
4
+ import type { TMappedResult } from '../mapped/index.mjs';
5
+ import { type TOptionalFromMappedResult } from './optional-from-mapped-result.mjs';
6
+ type RemoveOptional<T extends TSchema> = T extends TOptional<infer S> ? S : T;
7
+ declare function RemoveOptional<T extends TSchema>(schema: T): Record<PropertyKey, any>;
8
+ type AddOptional<T extends TSchema> = T extends TOptional<infer S> ? TOptional<S> : Ensure<TOptional<T>>;
9
+ declare function AddOptional<T extends TSchema>(schema: T): T & {
10
+ [OptionalKind]: string;
11
+ };
12
+ export type TOptionalWithFlag<T extends TSchema, F extends boolean> = F extends false ? RemoveOptional<T> : AddOptional<T>;
3
13
  export type TOptional<T extends TSchema> = T & {
4
14
  [OptionalKind]: 'Optional';
5
15
  };
6
- /** `[Json]` Creates an Optional property */
7
- export declare function Optional<T extends TSchema>(schema: T): TOptional<T>;
16
+ /** `[Json]` Creates a Optional property */
17
+ export declare function Optional<T extends TMappedResult, F extends boolean>(schema: T, enable: F): TOptionalFromMappedResult<T, F>;
18
+ /** `[Json]` Creates a Optional property */
19
+ export declare function Optional<T extends TSchema, F extends boolean>(schema: T, enable: F): TOptionalWithFlag<T, F>;
20
+ /** `[Json]` Creates a Optional property */
21
+ export declare function Optional<T extends TMappedResult>(schema: T): TOptionalFromMappedResult<T, true>;
22
+ /** `[Json]` Creates a Optional property */
23
+ export declare function Optional<T extends TSchema>(schema: T): TOptionalWithFlag<T, true>;
24
+ export {};
@@ -1,6 +1,22 @@
1
1
  import { OptionalKind } from '../symbols/index.mjs';
2
2
  import { CloneType } from '../clone/type.mjs';
3
- /** `[Json]` Creates an Optional property */
4
- export function Optional(schema) {
3
+ import { Discard } from '../discard/index.mjs';
4
+ import { OptionalFromMappedResult } from './optional-from-mapped-result.mjs';
5
+ import { IsMappedResult } from '../guard/type.mjs';
6
+ function RemoveOptional(schema) {
7
+ return Discard(CloneType(schema), [OptionalKind]);
8
+ }
9
+ function AddOptional(schema) {
5
10
  return { ...CloneType(schema), [OptionalKind]: 'Optional' };
6
11
  }
12
+ // prettier-ignore
13
+ function OptionalWithFlag(schema, F) {
14
+ return (F === false
15
+ ? RemoveOptional(schema)
16
+ : AddOptional(schema));
17
+ }
18
+ /** `[Json]` Creates a Optional property */
19
+ export function Optional(schema, enable) {
20
+ const F = enable ?? true;
21
+ return IsMappedResult(schema) ? OptionalFromMappedResult(schema, F) : OptionalWithFlag(schema, F);
22
+ }
@@ -1 +1,2 @@
1
+ export * from './readonly-from-mapped-result.mjs';
1
2
  export * from './readonly.mjs';
@@ -1 +1,2 @@
1
+ export * from './readonly-from-mapped-result.mjs';
1
2
  export * from './readonly.mjs';
@@ -0,0 +1,12 @@
1
+ import type { TProperties } from '../object/index.mjs';
2
+ import { type TMappedResult } from '../mapped/index.mjs';
3
+ import { type TReadonlyWithFlag } from './readonly.mjs';
4
+ type FromProperties<K extends TProperties, F extends boolean> = ({
5
+ [K2 in keyof K]: TReadonlyWithFlag<K[K2], F>;
6
+ });
7
+ declare function FromProperties<K extends TProperties, F extends boolean>(K: K, F: F): FromProperties<K, F>;
8
+ type FromMappedResult<K extends TMappedResult, F extends boolean> = (FromProperties<K['properties'], F>);
9
+ declare function FromMappedResult<K extends TMappedResult, F extends boolean>(K: K, F: F): FromMappedResult<K, F>;
10
+ export type TReadonlyFromMappedResult<T extends TMappedResult, F extends boolean, P extends TProperties = FromMappedResult<T, F>> = (TMappedResult<P>);
11
+ export declare function ReadonlyFromMappedResult<T extends TMappedResult, F extends boolean, P extends TProperties = FromMappedResult<T, F>>(T: T, F: F): TMappedResult<P>;
12
+ export {};
@@ -0,0 +1,17 @@
1
+ import { MappedResult } from '../mapped/index.mjs';
2
+ import { Readonly } from './readonly.mjs';
3
+ // prettier-ignore
4
+ function FromProperties(K, F) {
5
+ return globalThis.Object.getOwnPropertyNames(K).reduce((Acc, K2) => {
6
+ return { ...Acc, [K2]: Readonly(K[K2], F) };
7
+ }, {});
8
+ }
9
+ // prettier-ignore
10
+ function FromMappedResult(K, F) {
11
+ return FromProperties(K.properties, F);
12
+ }
13
+ // prettier-ignore
14
+ export function ReadonlyFromMappedResult(T, F) {
15
+ const P = FromMappedResult(T, F);
16
+ return MappedResult(P);
17
+ }
@@ -1,7 +1,24 @@
1
1
  import type { TSchema } from '../schema/index.mjs';
2
+ import type { Ensure } from '../helpers/index.mjs';
2
3
  import { ReadonlyKind } from '../symbols/index.mjs';
4
+ import type { TMappedResult } from '../mapped/index.mjs';
5
+ import { type TReadonlyFromMappedResult } from './readonly-from-mapped-result.mjs';
6
+ type RemoveReadonly<T extends TSchema> = T extends TReadonly<infer S> ? S : T;
7
+ declare function RemoveReadonly<T extends TSchema>(schema: T): Record<PropertyKey, any>;
8
+ type AddReadonly<T extends TSchema> = T extends TReadonly<infer S> ? TReadonly<S> : Ensure<TReadonly<T>>;
9
+ declare function AddReadonly<T extends TSchema>(schema: T): T & {
10
+ [ReadonlyKind]: string;
11
+ };
12
+ export type TReadonlyWithFlag<T extends TSchema, F extends boolean> = F extends false ? RemoveReadonly<T> : AddReadonly<T>;
3
13
  export type TReadonly<T extends TSchema> = T & {
4
14
  [ReadonlyKind]: 'Readonly';
5
15
  };
6
16
  /** `[Json]` Creates a Readonly property */
7
- export declare function Readonly<T extends TSchema>(schema: T): TReadonly<T>;
17
+ export declare function Readonly<T extends TMappedResult, F extends boolean>(schema: T, enable: F): TReadonlyFromMappedResult<T, F>;
18
+ /** `[Json]` Creates a Readonly property */
19
+ export declare function Readonly<T extends TSchema, F extends boolean>(schema: T, enable: F): TReadonlyWithFlag<T, F>;
20
+ /** `[Json]` Creates a Readonly property */
21
+ export declare function Readonly<T extends TMappedResult>(schema: T): TReadonlyFromMappedResult<T, true>;
22
+ /** `[Json]` Creates a Readonly property */
23
+ export declare function Readonly<T extends TSchema>(schema: T): TReadonlyWithFlag<T, true>;
24
+ export {};
@@ -1,6 +1,22 @@
1
1
  import { ReadonlyKind } from '../symbols/index.mjs';
2
2
  import { CloneType } from '../clone/type.mjs';
3
- /** `[Json]` Creates a Readonly property */
4
- export function Readonly(schema) {
3
+ import { Discard } from '../discard/index.mjs';
4
+ import { ReadonlyFromMappedResult } from './readonly-from-mapped-result.mjs';
5
+ import { IsMappedResult } from '../guard/type.mjs';
6
+ function RemoveReadonly(schema) {
7
+ return Discard(CloneType(schema), [ReadonlyKind]);
8
+ }
9
+ function AddReadonly(schema) {
5
10
  return { ...CloneType(schema), [ReadonlyKind]: 'Readonly' };
6
11
  }
12
+ // prettier-ignore
13
+ function ReadonlyWithFlag(schema, F) {
14
+ return (F === false
15
+ ? RemoveReadonly(schema)
16
+ : AddReadonly(schema));
17
+ }
18
+ /** `[Json]` Creates a Readonly property */
19
+ export function Readonly(schema, enable) {
20
+ const F = enable ?? true;
21
+ return IsMappedResult(schema) ? ReadonlyFromMappedResult(schema, F) : ReadonlyWithFlag(schema, F);
22
+ }
@@ -9,13 +9,13 @@ import { type TString } from '../string/index.mjs';
9
9
  import { type TInteger } from '../integer/index.mjs';
10
10
  import { type TNumber } from '../number/index.mjs';
11
11
  import { type TEnum } from '../enum/index.mjs';
12
- import { TTemplateLiteral, IsTemplateLiteralFinite } from '../template-literal/index.mjs';
12
+ import { TTemplateLiteral, TIsTemplateLiteralFinite } from '../template-literal/index.mjs';
13
13
  import { Kind } from '../symbols/index.mjs';
14
14
  type FromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
15
15
  type FromTemplateLiteralKeyFinite<K extends TTemplateLiteral, T extends TSchema, I extends string = Static<K>> = (Ensure<TObject<Evaluate<{
16
16
  [_ in I]: T;
17
17
  }>>>);
18
- type FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends false ? FromTemplateLiteralKeyInfinite<K, T> : FromTemplateLiteralKeyFinite<K, T>;
18
+ type FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> = TIsTemplateLiteralFinite<K> extends false ? FromTemplateLiteralKeyInfinite<K, T> : FromTemplateLiteralKeyFinite<K, T>;
19
19
  declare function FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema>(K: K, T: T, options: ObjectOptions): FromTemplateLiteralKey<K, T>;
20
20
  type FromEnumKey<K extends Record<string, string | number>, T extends TSchema> = Ensure<TObject<{
21
21
  [_ in K[keyof K]]: T;
@@ -1,7 +1,7 @@
1
1
  import { Object } from '../object/index.mjs';
2
2
  import { Never } from '../never/index.mjs';
3
3
  import { Union } from '../union/index.mjs';
4
- import { IsTemplateLiteralFinite, TemplateLiteralParseExact } from '../template-literal/index.mjs';
4
+ import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
5
5
  import { PatternStringExact, PatternNumberExact } from '../patterns/index.mjs';
6
6
  import { IndexPropertyKeys } from '../indexed/index.mjs';
7
7
  import { Kind, Hint } from '../symbols/index.mjs';
@@ -34,8 +34,7 @@ function RecordCreateFromKeys(K, T, options) {
34
34
  }
35
35
  // prettier-ignore
36
36
  function FromTemplateLiteralKey(K, T, options) {
37
- const expression = TemplateLiteralParseExact(K.pattern);
38
- return (IsTemplateLiteralFinite(expression)
37
+ return (IsTemplateLiteralFinite(K)
39
38
  ? RecordCreateFromKeys(IndexPropertyKeys(K), T, options)
40
39
  : RecordCreateFromPattern(K.pattern, T, options));
41
40
  }
@@ -20,7 +20,7 @@ export interface SchemaOptions {
20
20
  export interface TKind {
21
21
  [Kind]: string;
22
22
  }
23
- export interface TSchema extends SchemaOptions, TKind {
23
+ export interface TSchema extends TKind, SchemaOptions {
24
24
  [ReadonlyKind]?: string;
25
25
  [OptionalKind]?: string;
26
26
  [Hint]?: string;
@@ -1,6 +1,6 @@
1
1
  import { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import { Kind } from '../symbols/index.mjs';
3
- export type SymbolValue = string | number | undefined;
3
+ export type TSymbolValue = string | number | undefined;
4
4
  export interface TSymbol extends TSchema, SchemaOptions {
5
5
  [Kind]: 'Symbol';
6
6
  static: symbol;
@@ -6,11 +6,13 @@ import type { TNumber } from '../number/index.mjs';
6
6
  import type { TInteger } from '../integer/index.mjs';
7
7
  import type { TBigInt } from '../bigint/index.mjs';
8
8
  import type { TLiteral } from '../literal/index.mjs';
9
- import type { Expression } from './parser.mjs';
9
+ import type { Expression } from './parse.mjs';
10
10
  export declare class TemplateLiteralFiniteError extends Error {
11
11
  }
12
- type IsTemplateLiteralFiniteCheck<T> = T extends TTemplateLiteral<infer U extends TTemplateLiteralKind[]> ? IsTemplateLiteralFiniteArray<U> : T extends TUnion<infer U extends TTemplateLiteralKind[]> ? IsTemplateLiteralFiniteArray<U> : T extends TString ? false : T extends TBoolean ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TLiteral ? true : false;
13
- type IsTemplateLiteralFiniteArray<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? IsTemplateLiteralFiniteCheck<L> extends false ? false : IsTemplateLiteralFiniteArray<R> : true;
14
- export type IsTemplateLiteralFinite<T> = T extends TTemplateLiteral<infer U> ? IsTemplateLiteralFiniteArray<U> : false;
15
- export declare function IsTemplateLiteralFinite(expression: Expression): boolean;
12
+ type FromTemplateLiteralKind<T> = T extends TTemplateLiteral<infer U extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<U> : T extends TUnion<infer U extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<U> : T extends TString ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TBoolean ? true : T extends TLiteral ? true : false;
13
+ type FromTemplateLiteralKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? FromTemplateLiteralKind<L> extends false ? false : FromTemplateLiteralKinds<R> : true;
14
+ export declare function IsTemplateLiteralExpressionFinite(expression: Expression): boolean;
15
+ export type TIsTemplateLiteralFinite<T> = T extends TTemplateLiteral<infer U> ? FromTemplateLiteralKinds<U> : false;
16
+ /** Returns true if this TemplateLiteral resolves to a finite set of values */
17
+ export declare function IsTemplateLiteralFinite<T extends TTemplateLiteral>(schema: T): boolean;
16
18
  export {};
@@ -1,3 +1,4 @@
1
+ import { TemplateLiteralParseExact } from './parse.mjs';
1
2
  // ------------------------------------------------------------------
2
3
  // TemplateLiteralFiniteError
3
4
  // ------------------------------------------------------------------
@@ -28,12 +29,20 @@ function IsBooleanExpression(expression) {
28
29
  function IsStringExpression(expression) {
29
30
  return expression.type === 'const' && expression.const === '.*';
30
31
  }
32
+ // ------------------------------------------------------------------
33
+ // IsTemplateLiteralExpressionFinite
34
+ // ------------------------------------------------------------------
31
35
  // prettier-ignore
32
- export function IsTemplateLiteralFinite(expression) {
33
- return (IsBooleanExpression(expression) ? true :
34
- IsNumberExpression(expression) || IsStringExpression(expression) ? false :
35
- (expression.type === 'and') ? expression.expr.every((expr) => IsTemplateLiteralFinite(expr)) :
36
- (expression.type === 'or') ? expression.expr.every((expr) => IsTemplateLiteralFinite(expr)) :
36
+ export function IsTemplateLiteralExpressionFinite(expression) {
37
+ return (IsNumberExpression(expression) || IsStringExpression(expression) ? false :
38
+ IsBooleanExpression(expression) ? true :
39
+ (expression.type === 'and') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
40
+ (expression.type === 'or') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
37
41
  (expression.type === 'const') ? true :
38
42
  (() => { throw new TemplateLiteralFiniteError(`Unknown expression type`); })());
39
43
  }
44
+ /** Returns true if this TemplateLiteral resolves to a finite set of values */
45
+ export function IsTemplateLiteralFinite(schema) {
46
+ const expression = TemplateLiteralParseExact(schema.pattern);
47
+ return IsTemplateLiteralExpressionFinite(expression);
48
+ }
@@ -1,15 +1,21 @@
1
+ import { TIsTemplateLiteralFinite } from './finite.mjs';
2
+ import type { Assert } from '../helpers/index.mjs';
3
+ import type { TBoolean } from '../boolean/index.mjs';
1
4
  import type { TTemplateLiteral, TTemplateLiteralKind } from './index.mjs';
2
5
  import type { TLiteral, TLiteralValue } from '../literal/index.mjs';
3
- import type { Expression } from './parser.mjs';
6
+ import type { Expression } from './parse.mjs';
4
7
  import type { TUnion } from '../union/index.mjs';
8
+ export declare class TemplateLiteralGenerateError extends Error {
9
+ }
5
10
  type StringReduceUnary<L extends string, R extends string[]> = R extends [infer A extends string, ...infer B extends string[]] ? [`${L}${A}`, ...StringReduceUnary<L, B>] : [];
6
11
  type StringReduceBinary<L extends string[], R extends string[]> = L extends [infer A extends string, ...infer B extends string[]] ? [...StringReduceUnary<A, R>, ...StringReduceBinary<B, R>] : [];
7
12
  type StringReduceMany<T extends string[][]> = T extends [infer L extends string[], infer R extends string[], ...infer Rest extends string[][]] ? StringReduceMany<[StringReduceBinary<L, R>, ...Rest]> : T;
8
- type StringReduce<T extends string[][], O = StringReduceMany<T>> = 0 extends keyof O ? O[0] : [];
9
- type TemplateLiteralReduceUnion<T extends TTemplateLiteralKind[]> = T extends [infer L extends TLiteral, ...infer R extends TLiteral[]] ? [L['const'], ...TemplateLiteralReduceUnion<R>] : [];
10
- type TemplateLiteralReduce<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? L extends TLiteral<infer S extends TLiteralValue> ? [[S], ...TemplateLiteralReduce<R>] : L extends TUnion<infer S extends TTemplateLiteralKind[]> ? [TemplateLiteralReduceUnion<S>, ...TemplateLiteralReduce<R>] : [] : [];
11
- export declare class TemplateLiteralGenerateError extends Error {
12
- }
13
- export type TemplateLiteralGenerate<T> = T extends TTemplateLiteral<infer S extends TTemplateLiteralKind[]> ? TemplateLiteralReduce<S> extends infer R extends string[][] ? StringReduce<R> : [] : [];
14
- export declare function TemplateLiteralGenerate(expression: Expression): IterableIterator<string>;
13
+ type StringReduce<T extends string[][], O = StringReduceMany<T>> = 0 extends keyof O ? Assert<O[0], string[]> : [];
14
+ type FromTemplateLiteralUnionKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TLiteral, ...infer R extends TLiteral[]] ? [L['const'], ...FromTemplateLiteralUnionKinds<R>] : [];
15
+ type FromTemplateLiteralKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? (L extends TLiteral<infer S extends TLiteralValue> ? [[S], ...FromTemplateLiteralKinds<R>] : L extends TUnion<infer S extends TTemplateLiteralKind[]> ? [FromTemplateLiteralUnionKinds<S>, ...FromTemplateLiteralKinds<R>] : L extends TBoolean ? [['true', 'false'], ...FromTemplateLiteralKinds<R>] : [
16
+ ]) : [];
17
+ export declare function TemplateLiteralExpressionGenerate(expression: Expression): IterableIterator<string>;
18
+ export type TTemplateLiteralGenerate<T extends TTemplateLiteral, F = TIsTemplateLiteralFinite<T>> = F extends true ? (T extends TTemplateLiteral<infer S extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<S> extends infer R extends string[][] ? StringReduce<R> : [] : []) : [];
19
+ /** Generates a tuple of strings from the given TemplateLiteral. Returns an empty tuple if infinite. */
20
+ export declare function TemplateLiteralGenerate<T extends TTemplateLiteral>(schema: T): TTemplateLiteralGenerate<T>;
15
21
  export {};
@@ -1,5 +1,12 @@
1
+ import { IsTemplateLiteralExpressionFinite } from './finite.mjs';
2
+ import { TemplateLiteralParseExact } from './parse.mjs';
1
3
  // ------------------------------------------------------------------
2
- // GenerateFromExpression
4
+ // TemplateLiteralGenerateError
5
+ // ------------------------------------------------------------------
6
+ export class TemplateLiteralGenerateError extends Error {
7
+ }
8
+ // ------------------------------------------------------------------
9
+ // TemplateLiteralExpressionGenerate
3
10
  // ------------------------------------------------------------------
4
11
  // prettier-ignore
5
12
  function* GenerateReduce(buffer) {
@@ -13,23 +20,18 @@ function* GenerateReduce(buffer) {
13
20
  }
14
21
  // prettier-ignore
15
22
  function* GenerateAnd(expression) {
16
- return yield* GenerateReduce(expression.expr.map((expr) => [...TemplateLiteralGenerate(expr)]));
23
+ return yield* GenerateReduce(expression.expr.map((expr) => [...TemplateLiteralExpressionGenerate(expr)]));
17
24
  }
18
25
  // prettier-ignore
19
26
  function* GenerateOr(expression) {
20
27
  for (const expr of expression.expr)
21
- yield* TemplateLiteralGenerate(expr);
28
+ yield* TemplateLiteralExpressionGenerate(expr);
22
29
  }
23
30
  // prettier-ignore
24
31
  function* GenerateConst(expression) {
25
32
  return yield expression.const;
26
33
  }
27
- // ------------------------------------------------------------------
28
- // TemplateLiteralGenerateError
29
- // ------------------------------------------------------------------
30
- export class TemplateLiteralGenerateError extends Error {
31
- }
32
- export function* TemplateLiteralGenerate(expression) {
34
+ export function* TemplateLiteralExpressionGenerate(expression) {
33
35
  return expression.type === 'and'
34
36
  ? yield* GenerateAnd(expression)
35
37
  : expression.type === 'or'
@@ -40,3 +42,11 @@ export function* TemplateLiteralGenerate(expression) {
40
42
  throw new TemplateLiteralGenerateError('Unknown expression');
41
43
  })();
42
44
  }
45
+ /** Generates a tuple of strings from the given TemplateLiteral. Returns an empty tuple if infinite. */
46
+ export function TemplateLiteralGenerate(schema) {
47
+ const expression = TemplateLiteralParseExact(schema.pattern);
48
+ // prettier-ignore
49
+ return (IsTemplateLiteralExpressionFinite(expression)
50
+ ? [...TemplateLiteralExpressionGenerate(expression)]
51
+ : []);
52
+ }
@@ -1,7 +1,7 @@
1
1
  export * from './finite.mjs';
2
2
  export * from './generate.mjs';
3
3
  export * from './syntax.mjs';
4
- export * from './parser.mjs';
4
+ export * from './parse.mjs';
5
5
  export * from './pattern.mjs';
6
6
  export * from './union.mjs';
7
7
  export * from './template-literal.mjs';
@@ -1,7 +1,7 @@
1
1
  export * from './finite.mjs';
2
2
  export * from './generate.mjs';
3
3
  export * from './syntax.mjs';
4
- export * from './parser.mjs';
4
+ export * from './parse.mjs';
5
5
  export * from './pattern.mjs';
6
6
  export * from './union.mjs';
7
7
  export * from './template-literal.mjs';
@@ -127,16 +127,21 @@ function And(pattern) {
127
127
  (expressions.length === 1) ? expressions[0] :
128
128
  { type: 'and', expr: expressions });
129
129
  }
130
+ // ------------------------------------------------------------------
131
+ // TemplateLiteralParse
132
+ // ------------------------------------------------------------------
130
133
  /** Parses a pattern and returns an expression tree */
131
- // prettier-ignore
132
134
  export function TemplateLiteralParse(pattern) {
135
+ // prettier-ignore
133
136
  return (IsGroup(pattern) ? TemplateLiteralParse(InGroup(pattern)) :
134
137
  IsPrecedenceOr(pattern) ? Or(pattern) :
135
138
  IsPrecedenceAnd(pattern) ? And(pattern) :
136
139
  { type: 'const', const: pattern });
137
140
  }
141
+ // ------------------------------------------------------------------
142
+ // TemplateLiteralParseExact
143
+ // ------------------------------------------------------------------
138
144
  /** Parses a pattern and strips forward and trailing ^ and $ */
139
- // prettier-ignore
140
145
  export function TemplateLiteralParseExact(pattern) {
141
146
  return TemplateLiteralParse(pattern.slice(1, pattern.length - 1));
142
147
  }
@@ -14,7 +14,7 @@ type FromUnion<T extends string> = UnionEvaluated<FromUnionLiteral<T>>;
14
14
  type FromTerminal<T extends string> = T extends 'boolean' ? TBoolean : T extends 'bigint' ? TBigInt : T extends 'number' ? TNumber : T extends 'string' ? TString : FromUnion<T>;
15
15
  type FromString<T extends string> = T extends `{${infer L}}${infer R}` ? [FromTerminal<L>, ...FromString<R>] : T extends `${infer L}$${infer R}` ? [TLiteral<L>, ...FromString<R>] : T extends `${infer L}` ? [TLiteral<L>] : [
16
16
  ];
17
- export type TemplateLiteralSyntax<T extends string> = (TTemplateLiteral<Assert<FromString<T>, TTemplateLiteralKind[]>>);
17
+ export type TTemplateLiteralSyntax<T extends string> = (TTemplateLiteral<Assert<FromString<T>, TTemplateLiteralKind[]>>);
18
18
  /** Parses TemplateLiteralSyntax and returns a tuple of TemplateLiteralKinds */
19
19
  export declare function TemplateLiteralSyntax(syntax: string): TTemplateLiteralKind[];
20
20
  export {};
@@ -49,7 +49,6 @@ function* FromSyntax(syntax) {
49
49
  }
50
50
  yield Literal(syntax);
51
51
  }
52
- // prettier-ignore
53
52
  /** Parses TemplateLiteralSyntax and returns a tuple of TemplateLiteralKinds */
54
53
  export function TemplateLiteralSyntax(syntax) {
55
54
  return [...FromSyntax(syntax)];
@@ -9,7 +9,7 @@ import type { TString } from '../string/index.mjs';
9
9
  import type { TBoolean } from '../boolean/index.mjs';
10
10
  import type { TNever } from '../never/index.mjs';
11
11
  import type { Static } from '../static/index.mjs';
12
- import { TemplateLiteralSyntax } from './syntax.mjs';
12
+ import { TTemplateLiteralSyntax } from './syntax.mjs';
13
13
  import { EmptyString } from '../helpers/index.mjs';
14
14
  import { Kind } from '../symbols/index.mjs';
15
15
  type TemplateLiteralStaticKind<T, Acc extends string> = T extends TUnion<infer U> ? {
@@ -24,7 +24,7 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
24
24
  pattern: string;
25
25
  }
26
26
  /** `[Json]` Creates a TemplateLiteral type from template dsl string */
27
- export declare function TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions): TemplateLiteralSyntax<T>;
27
+ export declare function TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions): TTemplateLiteralSyntax<T>;
28
28
  /** `[Json]` Creates a TemplateLiteral type */
29
29
  export declare function TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
30
30
  export {};
@@ -1,5 +1,5 @@
1
- import { TemplateLiteralPattern } from './pattern.mjs';
2
1
  import { TemplateLiteralSyntax } from './syntax.mjs';
2
+ import { TemplateLiteralPattern } from './pattern.mjs';
3
3
  import { IsString } from '../guard/value.mjs';
4
4
  import { Kind } from '../symbols/index.mjs';
5
5
  /** `[Json]` Creates a TemplateLiteral type */
@@ -3,5 +3,5 @@ import { type TUnion } from '../union/index.mjs';
3
3
  import { type TLiteral } from '../literal/index.mjs';
4
4
  import { type TString } from '../string/index.mjs';
5
5
  import { type TNever } from '../never/index.mjs';
6
- /** Resolves a template literal as a TUnion */
7
- export declare function TemplateLiteralToUnion(template: TTemplateLiteral): TNever | TString | TUnion<TLiteral[]>;
6
+ /** Returns a Union from the given TemplateLiteral */
7
+ export declare function TemplateLiteralToUnion(schema: TTemplateLiteral): TNever | TString | TUnion<TLiteral[]>;
@@ -1,17 +1,12 @@
1
1
  import { Union } from '../union/index.mjs';
2
2
  import { Literal } from '../literal/index.mjs';
3
- import { String } from '../string/index.mjs';
4
3
  import { TemplateLiteralGenerate } from './generate.mjs';
5
- import { TemplateLiteralParseExact } from './parser.mjs';
6
- import { IsTemplateLiteralFinite } from './finite.mjs';
7
4
  // ------------------------------------------------------------------
8
5
  // TemplateLiteralToUnion
9
6
  // ------------------------------------------------------------------
10
- /** Resolves a template literal as a TUnion */
11
- export function TemplateLiteralToUnion(template) {
12
- const expression = TemplateLiteralParseExact(template.pattern);
13
- if (!IsTemplateLiteralFinite(expression))
14
- return String();
15
- const literals = [...TemplateLiteralGenerate(expression)].map((value) => Literal(value));
16
- return Union(literals);
7
+ /** Returns a Union from the given TemplateLiteral */
8
+ export function TemplateLiteralToUnion(schema) {
9
+ const R = TemplateLiteralGenerate(schema);
10
+ const L = R.map((S) => Literal(S));
11
+ return Union(L);
17
12
  }