diff options
| author | Tetsuharu Ohzeki <tetsuharu.ohzeki@gmail.com> | 2023-06-28 04:03:53 +0900 |
|---|---|---|
| committer | Tetsuharu Ohzeki <tetsuharu.ohzeki@gmail.com> | 2023-07-06 16:17:02 +0900 |
| commit | 72a3883a7128b4eae6f38d6479f33aaaaae4790b (patch) | |
| tree | 60b9d6f755bbefeca335bcbc10d0832abc719c48 /editors/code/src/nullable.ts | |
| parent | bb35d8fa8ef9de3c8282602b411c40b266dc3a4e (diff) | |
| download | rust-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.ts | 19 |
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\``); +} |
