mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-23 02:27:39 +03:00
20 lines
557 B
TypeScript
20 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\``);
|
|
}
|