mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-11 04:38:49 +00:00
19 lines
557 B
TypeScript
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\``);
|
|
}
|