Skip to content

Commit 01deea7

Browse files
committed
use assertNever to ensure runtime switch statements are kept up to date
1 parent 7137880 commit 01deea7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Runtime/src/js-value.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Memory } from "./memory";
2-
import { pointer } from "./types";
2+
import { assertNever, pointer } from "./types";
33

44
export const enum Kind {
55
Boolean = 0,
@@ -44,7 +44,7 @@ export const decode = (
4444
return undefined;
4545

4646
default:
47-
throw new Error(`JSValue Type kind "${kind}" is not supported`);
47+
assertNever(kind, `JSValue Type kind "${kind}" is not supported`);
4848
}
4949
};
5050

@@ -81,7 +81,8 @@ export const write = (
8181
memory.writeUint32(payload1_ptr, memory.retain(value));
8282
};
8383

84-
switch (typeof value) {
84+
const type = typeof value;
85+
switch (type) {
8586
case "boolean": {
8687
memory.writeUint32(kind_ptr, exceptionBit | Kind.Boolean);
8788
memory.writeUint32(payload1_ptr, value ? 1 : 0);
@@ -117,6 +118,6 @@ export const write = (
117118
break;
118119
}
119120
default:
120-
throw new Error(`Type "${typeof value}" is not supported yet`);
121+
assertNever(type, `Type "${type}" is not supported yet`);
121122
}
122123
};

Runtime/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ export type TypedArray =
124124
// | BigUint64ArrayConstructor
125125
| Float32ArrayConstructor
126126
| Float64ArrayConstructor;
127+
128+
export function assertNever(x: never, message: string) {
129+
throw new Error(message);
130+
}

0 commit comments

Comments
 (0)