rust-analyzer/editors/code/src/undefinable.ts

19 lines
557 B
TypeScript

export type NotUndefined<T> = T extends undefined ? never : T;
export type Undefinable<T> = T | undefined;
function isNotUndefined<T>(input: Undefinable<T>): input is NotUndefined<T> {
return input !== undefined;
}
export function expectNotUndefined<T>(input: Undefinable<T>, msg: string): NotUndefined<T> {
if (isNotUndefined(input)) {
return input;
}
throw new TypeError(msg);
}
export function unwrapUndefinable<T>(input: Undefinable<T>): NotUndefined<T> {
return expectNotUndefined(input, `unwrapping \`undefined\``);
}