mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +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\``);
|
|
}
|