@infra-blocks/types 0.26.0 → 0.27.0-alpha.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.
@@ -5,6 +5,8 @@ export * from "./types.js";
5
5
  /**
6
6
  * A function for forcing exhaustiveness in switch statements or if-else chains.
7
7
  *
8
+ * Throws an assertion error at runtime if called. The message can be specified as the second argument.
9
+ *
8
10
  * @example
9
11
  * type BigType = "penus" | "penii";
10
12
  * function doStuff(x: BigType) {
@@ -23,7 +25,7 @@ export * from "./types.js";
23
25
  *
24
26
  * @param value - The value to validate against never.
25
27
  */
26
- export declare function unreachable(value: never): never;
28
+ export declare function unreachable(_: never, message?: string): never;
27
29
  /**
28
30
  * A function to mark a value as trusted for a type cast that can leverage inference.
29
31
  *
package/lib/cjs/index.js CHANGED
@@ -16,13 +16,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.unreachable = unreachable;
18
18
  exports.trusted = trusted;
19
+ const node_assert_1 = require("node:assert");
19
20
  __exportStar(require("./func.js"), exports);
20
21
  __exportStar(require("./guard.js"), exports);
21
22
  __exportStar(require("./predicates.js"), exports);
22
23
  __exportStar(require("./types.js"), exports);
24
+ // TODO: Message type, that is either: undefined, a string, a string followed by format args or a function returning a string.
23
25
  /**
24
26
  * A function for forcing exhaustiveness in switch statements or if-else chains.
25
27
  *
28
+ * Throws an assertion error at runtime if called. The message can be specified as the second argument.
29
+ *
26
30
  * @example
27
31
  * type BigType = "penus" | "penii";
28
32
  * function doStuff(x: BigType) {
@@ -41,8 +45,8 @@ __exportStar(require("./types.js"), exports);
41
45
  *
42
46
  * @param value - The value to validate against never.
43
47
  */
44
- function unreachable(value) {
45
- return value;
48
+ function unreachable(_, message) {
49
+ (0, node_assert_1.fail)(message ?? "unreachable code reached!");
46
50
  }
47
51
  /**
48
52
  * A function to mark a value as trusted for a type cast that can leverage inference.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AA0BA,kCAEC;AAsBD,0BAEC;AApDD,4CAA0B;AAC1B,6CAA2B;AAC3B,kDAAgC;AAChC,6CAA2B;AAE3B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,WAAW,CAAC,KAAY;IACtC,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,OAAO,CAAI,KAAc;IACvC,OAAO,KAAU,CAAC;AACpB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AA+BA,kCAEC;AAsBD,0BAEC;AAzDD,6CAAmC;AAEnC,4CAA0B;AAC1B,6CAA2B;AAC3B,kDAAgC;AAChC,6CAA2B;AAE3B,8HAA8H;AAC9H;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,WAAW,CAAC,CAAQ,EAAE,OAAgB;IACpD,IAAA,kBAAI,EAAC,OAAO,IAAI,2BAA2B,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,OAAO,CAAI,KAAc;IACvC,OAAO,KAAU,CAAC;AACpB,CAAC"}
@@ -58,6 +58,12 @@ export type Nullable<T> = T | null | undefined;
58
58
  * This is in contrast to the built-in `Partial` type which makes all fields optional.
59
59
  */
60
60
  export type Optional<T, K extends keyof T = keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
61
+ /**
62
+ * A utility type for tracking a phantom type parameter.
63
+ */
64
+ export type Phantom<T> = {
65
+ _phantom?: T;
66
+ };
61
67
  /**
62
68
  * A union type that includes all primitive types.
63
69
  */
@@ -5,6 +5,8 @@ export * from "./types.js";
5
5
  /**
6
6
  * A function for forcing exhaustiveness in switch statements or if-else chains.
7
7
  *
8
+ * Throws an assertion error at runtime if called. The message can be specified as the second argument.
9
+ *
8
10
  * @example
9
11
  * type BigType = "penus" | "penii";
10
12
  * function doStuff(x: BigType) {
@@ -23,7 +25,7 @@ export * from "./types.js";
23
25
  *
24
26
  * @param value - The value to validate against never.
25
27
  */
26
- export declare function unreachable(value: never): never;
28
+ export declare function unreachable(_: never, message?: string): never;
27
29
  /**
28
30
  * A function to mark a value as trusted for a type cast that can leverage inference.
29
31
  *
package/lib/esm/index.js CHANGED
@@ -1,10 +1,14 @@
1
+ import { fail } from "node:assert";
1
2
  export * from "./func.js";
2
3
  export * from "./guard.js";
3
4
  export * from "./predicates.js";
4
5
  export * from "./types.js";
6
+ // TODO: Message type, that is either: undefined, a string, a string followed by format args or a function returning a string.
5
7
  /**
6
8
  * A function for forcing exhaustiveness in switch statements or if-else chains.
7
9
  *
10
+ * Throws an assertion error at runtime if called. The message can be specified as the second argument.
11
+ *
8
12
  * @example
9
13
  * type BigType = "penus" | "penii";
10
14
  * function doStuff(x: BigType) {
@@ -23,8 +27,8 @@ export * from "./types.js";
23
27
  *
24
28
  * @param value - The value to validate against never.
25
29
  */
26
- export function unreachable(value) {
27
- return value;
30
+ export function unreachable(_, message) {
31
+ fail(message ?? "unreachable code reached!");
28
32
  }
29
33
  /**
30
34
  * A function to mark a value as trusted for a type cast that can leverage inference.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,WAAW,CAAC,KAAY;IACtC,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,OAAO,CAAI,KAAc;IACvC,OAAO,KAAU,CAAC;AACpB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAE3B,8HAA8H;AAC9H;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,WAAW,CAAC,CAAQ,EAAE,OAAgB;IACpD,IAAI,CAAC,OAAO,IAAI,2BAA2B,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,OAAO,CAAI,KAAc;IACvC,OAAO,KAAU,CAAC;AACpB,CAAC"}
@@ -58,6 +58,12 @@ export type Nullable<T> = T | null | undefined;
58
58
  * This is in contrast to the built-in `Partial` type which makes all fields optional.
59
59
  */
60
60
  export type Optional<T, K extends keyof T = keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
61
+ /**
62
+ * A utility type for tracking a phantom type parameter.
63
+ */
64
+ export type Phantom<T> = {
65
+ _phantom?: T;
66
+ };
61
67
  /**
62
68
  * A union type that includes all primitive types.
63
69
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infra-blocks/types",
3
- "version": "0.26.0",
3
+ "version": "0.27.0-alpha.1",
4
4
  "description": "Typescript types utility package.",
5
5
  "keywords": [
6
6
  "type",