From 72a3883a7128b4eae6f38d6479f33aaaaae4790b Mon Sep 17 00:00:00 2001 From: Tetsuharu Ohzeki Date: Wed, 28 Jun 2023 04:03:53 +0900 Subject: editor/code: Enable `noUncheckedIndexedAccess` ts option https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess --- editors/code/src/nullable.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 editors/code/src/nullable.ts (limited to 'editors/code/src/nullable.ts') 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 extends null ? never : T; + +export type Nullable = T | null; + +function isNotNull(input: Nullable): input is NotNull { + return input !== null; +} + +function expectNotNull(input: Nullable, msg: string): NotNull { + if (isNotNull(input)) { + return input; + } + + throw new TypeError(msg); +} + +export function unwrapNullable(input: Nullable): NotNull { + return expectNotNull(input, `unwrapping \`null\``); +} -- cgit 1.4.1-3-g733a5