about summary refs log tree commit diff
path: root/editors/code/src/nullable.ts
diff options
context:
space:
mode:
authorLaurențiu Nicola <lnicola@dend.ro>2023-07-17 16:09:39 +0300
committerLaurențiu Nicola <lnicola@dend.ro>2023-07-17 16:09:39 +0300
commit71499fcd2273ba05d2c6173591bf66113fa19a5f (patch)
treee5d7d3ad656222feb1b50014b7dc4e0de964b5a6 /editors/code/src/nullable.ts
parent6502421771dabc8a3b97c06b348fd9e04270e4f3 (diff)
parentd82451103962b1482cb137850c81a1acb34db0e7 (diff)
downloadrust-71499fcd2273ba05d2c6173591bf66113fa19a5f.tar.gz
rust-71499fcd2273ba05d2c6173591bf66113fa19a5f.zip
Merge remote-tracking branch 'upstream/master' into sync-from-rust
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\``);
+}