about summary refs log tree commit diff
path: root/editors/code/src/nullable.ts
diff options
context:
space:
mode:
authorTetsuharu Ohzeki <tetsuharu.ohzeki@gmail.com>2023-06-28 04:03:53 +0900
committerTetsuharu Ohzeki <tetsuharu.ohzeki@gmail.com>2023-07-06 16:17:02 +0900
commit72a3883a7128b4eae6f38d6479f33aaaaae4790b (patch)
tree60b9d6f755bbefeca335bcbc10d0832abc719c48 /editors/code/src/nullable.ts
parentbb35d8fa8ef9de3c8282602b411c40b266dc3a4e (diff)
downloadrust-72a3883a7128b4eae6f38d6479f33aaaaae4790b.tar.gz
rust-72a3883a7128b4eae6f38d6479f33aaaaae4790b.zip
editor/code: Enable `noUncheckedIndexedAccess` ts option
https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess
Diffstat (limited to 'editors/code/src/nullable.ts')
-rw-r--r--editors/code/src/nullable.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/editors/code/src/nullable.ts b/editors/code/src/nullable.ts
new file mode 100644
index 00000000000..e973e162907
--- /dev/null
+++ b/editors/code/src/nullable.ts
@@ -0,0 +1,19 @@
+export type NotNull<T> = T extends null ? never : T;
+
+export type Nullable<T> = T | null;
+
+function isNotNull<T>(input: Nullable<T>): input is NotNull<T> {
+    return input !== null;
+}
+
+function expectNotNull<T>(input: Nullable<T>, msg: string): NotNull<T> {
+    if (isNotNull(input)) {
+        return input;
+    }
+
+    throw new TypeError(msg);
+}
+
+export function unwrapNullable<T>(input: Nullable<T>): NotNull<T> {
+    return expectNotNull(input, `unwrapping \`null\``);
+}