diff options
| author | Laurențiu Nicola <lnicola@dend.ro> | 2023-07-17 16:09:39 +0300 |
|---|---|---|
| committer | Laurențiu Nicola <lnicola@dend.ro> | 2023-07-17 16:09:39 +0300 |
| commit | 71499fcd2273ba05d2c6173591bf66113fa19a5f (patch) | |
| tree | e5d7d3ad656222feb1b50014b7dc4e0de964b5a6 /editors/code/src/nullable.ts | |
| parent | 6502421771dabc8a3b97c06b348fd9e04270e4f3 (diff) | |
| parent | d82451103962b1482cb137850c81a1acb34db0e7 (diff) | |
| download | rust-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.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\``); +} |
