ts-fortress 2.8.0 → 2.9.0

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 (42) hide show
  1. package/dist/entry-point.mjs +1 -0
  2. package/dist/entry-point.mjs.map +1 -1
  3. package/dist/index.mjs +1 -0
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/other-types/index.d.mts +1 -0
  6. package/dist/other-types/index.d.mts.map +1 -1
  7. package/dist/other-types/index.mjs +1 -0
  8. package/dist/other-types/index.mjs.map +1 -1
  9. package/dist/other-types/literal.d.mts +3 -0
  10. package/dist/other-types/literal.d.mts.map +1 -0
  11. package/dist/other-types/literal.mjs +15 -0
  12. package/dist/other-types/literal.mjs.map +1 -0
  13. package/dist/primitives/bigint.d.mts +1 -0
  14. package/dist/primitives/bigint.d.mts.map +1 -1
  15. package/dist/primitives/bigint.mjs +1 -0
  16. package/dist/primitives/bigint.mjs.map +1 -1
  17. package/dist/primitives/boolean.d.mts +1 -0
  18. package/dist/primitives/boolean.d.mts.map +1 -1
  19. package/dist/primitives/boolean.mjs +1 -0
  20. package/dist/primitives/boolean.mjs.map +1 -1
  21. package/dist/primitives/number.d.mts +1 -0
  22. package/dist/primitives/number.d.mts.map +1 -1
  23. package/dist/primitives/number.mjs +1 -0
  24. package/dist/primitives/number.mjs.map +1 -1
  25. package/dist/primitives/string.d.mts +1 -0
  26. package/dist/primitives/string.d.mts.map +1 -1
  27. package/dist/primitives/string.mjs +1 -0
  28. package/dist/primitives/string.mjs.map +1 -1
  29. package/package.json +1 -1
  30. package/src/array/tuple.test.mts +6 -6
  31. package/src/compose/union.test.mts +8 -12
  32. package/src/other-types/index.mts +1 -0
  33. package/src/other-types/literal.mts +18 -0
  34. package/src/other-types/literal.test.mts +635 -0
  35. package/src/primitives/bigint.mts +1 -0
  36. package/src/primitives/bigint.test.mts +1 -137
  37. package/src/primitives/boolean.mts +1 -0
  38. package/src/primitives/boolean.test.mts +1 -220
  39. package/src/primitives/number.mts +1 -0
  40. package/src/primitives/number.test.mts +1 -116
  41. package/src/primitives/string.mts +1 -0
  42. package/src/primitives/string.test.mts +1 -160
@@ -29,6 +29,7 @@ export { union } from './compose/union.mjs';
29
29
  export { enumType } from './enum/enum.mjs';
30
30
  export { intRange } from './enum/int-range.mjs';
31
31
  export { uintRange } from './enum/uint-range.mjs';
32
+ export { literal } from './other-types/literal.mjs';
32
33
  export { unknown } from './other-types/unknown.mjs';
33
34
  export { int8 } from './predefined/int8.mjs';
34
35
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -28,6 +28,7 @@ export { union } from './compose/union.mjs';
28
28
  export { enumType } from './enum/enum.mjs';
29
29
  export { intRange } from './enum/int-range.mjs';
30
30
  export { uintRange } from './enum/uint-range.mjs';
31
+ export { literal } from './other-types/literal.mjs';
31
32
  export { unknown } from './other-types/unknown.mjs';
32
33
  export { int8 } from './predefined/int8.mjs';
33
34
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,3 @@
1
+ export * from './literal.mjs';
1
2
  export * from './unknown.mjs';
2
3
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/other-types/index.mts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/other-types/index.mts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
@@ -1,2 +1,3 @@
1
+ export { literal } from './literal.mjs';
1
2
  export { unknown } from './unknown.mjs';
2
3
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,3 @@
1
+ import { type Type } from '../type.mjs';
2
+ export declare const literal: <L extends Extract<Primitive, string | number | bigint | boolean>>(value: L) => Type<L>;
3
+ //# sourceMappingURL=literal.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"literal.d.mts","sourceRoot":"","sources":["../../src/other-types/literal.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,OAAO,GAClB,CAAC,SAAS,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAEhE,OAAO,CAAC,KACP,IAAI,CAAC,CAAC,CASL,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { isString, isBigint } from 'ts-data-forge';
2
+ import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
3
+
4
+ const literal = (value) => createPrimitiveType({
5
+ typeName: isString(value)
6
+ ? `literal("${value}")`
7
+ : isBigint(value)
8
+ ? `literal(${value}n)`
9
+ : `literal(${value})`,
10
+ defaultValue: value,
11
+ is: (u) => u === value,
12
+ });
13
+
14
+ export { literal };
15
+ //# sourceMappingURL=literal.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"literal.mjs","sources":["../../src/other-types/literal.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,OAAO,GAAG,CAGrB,KAAQ,KAER,mBAAmB,CAAC;AAClB,IAAA,QAAQ,EAAE,QAAQ,CAAC,KAAK;UACpB,CAAA,SAAA,EAAY,KAAK,CAAA,EAAA;AACnB,UAAE,QAAQ,CAAC,KAAK;cACZ,CAAA,QAAA,EAAW,KAAK,CAAA,EAAA;cAChB,CAAA,QAAA,EAAW,KAAK,CAAA,CAAA,CAAG;AACzB,IAAA,YAAY,EAAE,KAAK;IACnB,EAAE,EAAE,CAAC,CAAU,KAAa,CAAC,KAAK,KAAK;AACxC,CAAA;;;;"}
@@ -1,4 +1,5 @@
1
1
  import { type Type } from '../type.mjs';
2
2
  export declare const bigint: (defaultValue?: bigint) => Type<bigint>;
3
+ /** @deprecated Use `literal` instead. */
3
4
  export declare const bigintLiteral: <L extends bigint>(literal: L) => Type<L>;
4
5
  //# sourceMappingURL=bigint.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bigint.d.mts","sourceRoot":"","sources":["../../src/primitives/bigint.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAW,KAAG,IAAI,CAAC,MAAM,CACW,CAAC;AAE1E,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
1
+ {"version":3,"file":"bigint.d.mts","sourceRoot":"","sources":["../../src/primitives/bigint.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAW,KAAG,IAAI,CAAC,MAAM,CACW,CAAC;AAE1E,yCAAyC;AACzC,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
@@ -2,6 +2,7 @@ import { isBigint } from 'ts-data-forge';
2
2
  import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
3
3
 
4
4
  const bigint = (defaultValue = 0n) => createPrimitiveType({ typeName: 'bigint', defaultValue, is: isBigint });
5
+ /** @deprecated Use `literal` instead. */
5
6
  const bigintLiteral = (literal) => createPrimitiveType({
6
7
  typeName: `bigintLiteral(${literal})`,
7
8
  defaultValue: literal,
@@ -1 +1 @@
1
- {"version":3,"file":"bigint.mjs","sources":["../../src/primitives/bigint.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAEjE,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,cAAA,EAAiB,OAAO,CAAA,CAAA,CAAG;AACrC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
1
+ {"version":3,"file":"bigint.mjs","sources":["../../src/primitives/bigint.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAExE;AACO,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,cAAA,EAAiB,OAAO,CAAA,CAAA,CAAG;AACrC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
@@ -1,4 +1,5 @@
1
1
  import { type Type } from '../type.mjs';
2
2
  export declare const boolean: (defaultValue?: boolean) => Type<boolean>;
3
+ /** @deprecated Use `literal` instead. */
3
4
  export declare const booleanLiteral: <L extends boolean>(literal: L) => Type<L>;
4
5
  //# sourceMappingURL=boolean.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"boolean.d.mts","sourceRoot":"","sources":["../../src/primitives/boolean.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,OAAO,GAAI,eAAc,OAAe,KAAG,IAAI,CAAC,OAAO,CACO,CAAC;AAE5E,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,OAAO,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAKjE,CAAC"}
1
+ {"version":3,"file":"boolean.d.mts","sourceRoot":"","sources":["../../src/primitives/boolean.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,OAAO,GAAI,eAAc,OAAe,KAAG,IAAI,CAAC,OAAO,CACO,CAAC;AAE5E,yCAAyC;AACzC,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,OAAO,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAKjE,CAAC"}
@@ -2,6 +2,7 @@ import { isBoolean } from 'ts-data-forge';
2
2
  import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
3
3
 
4
4
  const boolean = (defaultValue = false) => createPrimitiveType({ typeName: 'boolean', defaultValue, is: isBoolean });
5
+ /** @deprecated Use `literal` instead. */
5
6
  const booleanLiteral = (literal) => createPrimitiveType({
6
7
  typeName: `booleanLiteral(${String(literal)})`,
7
8
  defaultValue: literal,
@@ -1 +1 @@
1
- {"version":3,"file":"boolean.mjs","sources":["../../src/primitives/boolean.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,OAAO,GAAG,CAAC,eAAwB,KAAK,KACnD,mBAAmB,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE;AAEnE,MAAM,cAAc,GAAG,CAAoB,OAAU,KAC1D,mBAAmB,CAAC;AAClB,IAAA,QAAQ,EAAE,CAAA,eAAA,EAAkB,MAAM,CAAC,OAAO,CAAC,CAAA,CAAA,CAAG;AAC9C,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
1
+ {"version":3,"file":"boolean.mjs","sources":["../../src/primitives/boolean.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,OAAO,GAAG,CAAC,eAAwB,KAAK,KACnD,mBAAmB,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE;AAE1E;AACO,MAAM,cAAc,GAAG,CAAoB,OAAU,KAC1D,mBAAmB,CAAC;AAClB,IAAA,QAAQ,EAAE,CAAA,eAAA,EAAkB,MAAM,CAAC,OAAO,CAAC,CAAA,CAAA,CAAG;AAC9C,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
@@ -1,4 +1,5 @@
1
1
  import { type Type } from '../type.mjs';
2
2
  export declare const number: (defaultValue?: number) => Type<number>;
3
+ /** @deprecated Use `literal` instead. */
3
4
  export declare const numberLiteral: <L extends number>(literal: L) => Type<L>;
4
5
  //# sourceMappingURL=number.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"number.d.mts","sourceRoot":"","sources":["../../src/primitives/number.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAU,KAAG,IAAI,CAAC,MAAM,CACY,CAAC;AAE1E,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
1
+ {"version":3,"file":"number.d.mts","sourceRoot":"","sources":["../../src/primitives/number.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAU,KAAG,IAAI,CAAC,MAAM,CACY,CAAC;AAE1E,yCAAyC;AACzC,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
@@ -2,6 +2,7 @@ import { isNumber } from 'ts-data-forge';
2
2
  import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
3
3
 
4
4
  const number = (defaultValue = 0) => createPrimitiveType({ typeName: 'number', defaultValue, is: isNumber });
5
+ /** @deprecated Use `literal` instead. */
5
6
  const numberLiteral = (literal) => createPrimitiveType({
6
7
  typeName: `numberLiteral(${literal})`,
7
8
  defaultValue: literal,
@@ -1 +1 @@
1
- {"version":3,"file":"number.mjs","sources":["../../src/primitives/number.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,CAAC,KAC7C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAEjE,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,cAAA,EAAiB,OAAO,CAAA,CAAA,CAAG;AACrC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
1
+ {"version":3,"file":"number.mjs","sources":["../../src/primitives/number.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,CAAC,KAC7C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAExE;AACO,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,cAAA,EAAiB,OAAO,CAAA,CAAA,CAAG;AACrC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
@@ -1,4 +1,5 @@
1
1
  import { type Type } from '../type.mjs';
2
2
  export declare const string: (defaultValue?: string) => Type<string>;
3
+ /** @deprecated Use `literal` instead. */
3
4
  export declare const stringLiteral: <L extends string>(literal: L) => Type<L>;
4
5
  //# sourceMappingURL=string.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"string.d.mts","sourceRoot":"","sources":["../../src/primitives/string.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAW,KAAG,IAAI,CAAC,MAAM,CACW,CAAC;AAE1E,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
1
+ {"version":3,"file":"string.d.mts","sourceRoot":"","sources":["../../src/primitives/string.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,MAAM,GAAI,eAAc,MAAW,KAAG,IAAI,CAAC,MAAM,CACW,CAAC;AAE1E,yCAAyC;AACzC,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,EAAE,SAAS,CAAC,KAAG,IAAI,CAAC,CAAC,CAK/D,CAAC"}
@@ -2,6 +2,7 @@ import { isString } from 'ts-data-forge';
2
2
  import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
3
3
 
4
4
  const string = (defaultValue = '') => createPrimitiveType({ typeName: 'string', defaultValue, is: isString });
5
+ /** @deprecated Use `literal` instead. */
5
6
  const stringLiteral = (literal) => createPrimitiveType({
6
7
  typeName: `stringLiteral("${literal}")`,
7
8
  defaultValue: literal,
@@ -1 +1 @@
1
- {"version":3,"file":"string.mjs","sources":["../../src/primitives/string.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAEjE,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,eAAA,EAAkB,OAAO,CAAA,EAAA,CAAI;AACvC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
1
+ {"version":3,"file":"string.mjs","sources":["../../src/primitives/string.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAExE;AACO,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,eAAA,EAAkB,OAAO,CAAA,EAAA,CAAI;AACvC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-fortress",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "typescript"
@@ -1,5 +1,6 @@
1
1
  import { expectType, Result } from 'ts-data-forge';
2
- import { number, numberLiteral, stringLiteral } from '../primitives/index.mjs';
2
+ import { literal } from '../other-types/index.mjs';
3
+ import { number } from '../primitives/index.mjs';
3
4
  import { record } from '../record/index.mjs';
4
5
  import { type TypeOf } from '../type.mjs';
5
6
  import {
@@ -12,14 +13,13 @@ describe('tuple', () => {
12
13
  describe('arg patterns', () => {
13
14
  test('without explicit default value', () => {
14
15
  expect(
15
- tuple([numberLiteral(1), numberLiteral(2), numberLiteral(3)])
16
- .defaultValue,
16
+ tuple([literal(1), literal(2), literal(3)]).defaultValue,
17
17
  ).toStrictEqual([1, 2, 3]);
18
18
  });
19
19
 
20
20
  test('with explicit default value', () => {
21
21
  expect(
22
- tuple([numberLiteral(1), numberLiteral(2), numberLiteral(3)], {
22
+ tuple([literal(1), literal(2), literal(3)], {
23
23
  typeName: 'tpl',
24
24
  }).typeName,
25
25
  ).toBe('tpl');
@@ -28,8 +28,8 @@ describe('tuple', () => {
28
28
 
29
29
  const targetType = tuple([
30
30
  record({ x: number(0), y: number(0) }),
31
- numberLiteral(3),
32
- stringLiteral('2'),
31
+ literal(3),
32
+ literal('2'),
33
33
  ]);
34
34
 
35
35
  type TargetType = TypeOf<typeof targetType>;
@@ -1,5 +1,6 @@
1
1
  import { expectType, Result } from 'ts-data-forge';
2
- import { number, numberLiteral, stringLiteral } from '../primitives/index.mjs';
2
+ import { literal } from '../other-types/index.mjs';
3
+ import { number } from '../primitives/index.mjs';
3
4
  import { record } from '../record/index.mjs';
4
5
  import { type TypeOf } from '../type.mjs';
5
6
  import {
@@ -10,13 +11,9 @@ import { union } from './union.mjs';
10
11
 
11
12
  describe('union', () => {
12
13
  const targetType = union(
13
- [
14
- record({ x: number(0), y: number(0) }),
15
- numberLiteral(3),
16
- stringLiteral('2'),
17
- ],
14
+ [record({ x: number(0), y: number(0) }), literal(3), literal('2')],
18
15
  {
19
- defaultType: numberLiteral(3),
16
+ defaultType: literal(3),
20
17
  },
21
18
  );
22
19
 
@@ -94,14 +91,13 @@ describe('union', () => {
94
91
  path: [],
95
92
  actualValue: 5,
96
93
  expectedType:
97
- 'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral("2")>',
98
- typeName:
99
- 'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral("2")>',
94
+ 'Union<{ x: number, y: number }, literal(3), literal("2")>',
95
+ typeName: 'Union<{ x: number, y: number }, literal(3), literal("2")>',
100
96
  message:
101
- 'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral("2") }',
97
+ 'The type of value is expected to be one of the elements contained in { { x: number, y: number }, literal(3), literal("2") }',
102
98
  });
103
99
  expect(validationErrorsToMessages(result.value)).toStrictEqual([
104
- 'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral("2") }',
100
+ 'The type of value is expected to be one of the elements contained in { { x: number, y: number }, literal(3), literal("2") }',
105
101
  ]);
106
102
  }
107
103
  });
@@ -1 +1,2 @@
1
+ export * from './literal.mjs';
1
2
  export * from './unknown.mjs';
@@ -0,0 +1,18 @@
1
+ import { isBigint, isString } from 'ts-data-forge';
2
+ import { type Type } from '../type.mjs';
3
+ import { createPrimitiveType } from '../utils/index.mjs';
4
+
5
+ export const literal = <
6
+ L extends Extract<Primitive, string | number | bigint | boolean>,
7
+ >(
8
+ value: L,
9
+ ): Type<L> =>
10
+ createPrimitiveType({
11
+ typeName: isString(value)
12
+ ? `literal("${value}")`
13
+ : isBigint(value)
14
+ ? `literal(${value}n)`
15
+ : `literal(${value})`,
16
+ defaultValue: value,
17
+ is: (u: unknown): u is L => u === value,
18
+ });