🔮
Void How do you check if this variable contains a value?
let value: Value;
You may initialize it with undefined
or null
, but what if Value
is a generic you don't control, and it can be one of these values?
import { Void } from "ts-void";
You may use Void
as a new primitive value.
let value: Value | Void;
then you can check if the variable is Void
.
if (value !== Void) {
// Now the value is only of type "Value".
}
You may also use the utility type.
import { type NonVoid } from "ts-void";
And extract the original type from the union.
type Extracted = NonVoid<T | Void>; // equals to T.