Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
type Freezable = {
unfreezable: never;
};
type Frozen<T extends Freezable> = T & {
frozen: true;
};
function assertFreezable(thing: unknown): asserts thing is Freezable {
if (!thing || (thing as Freezable).unfreezable) {
throw new Error(`Can't freeze that! ❄️`);
}
}
function freeze<T extends Freezable>(toFreeze: T): Frozen<T> {
assertFreezable(toFreeze);
(toFreeze as Frozen<T>).frozen = true;
return toFreeze as Frozen<T>;
}
const thing = {};
const frozenThing = freeze(thing); //Argument of type '{}' is not assignable to parameter of type 'Freezable'.
//Property 'unfreezable' is missing in type '{}' but required in type 'Freezable'.ts(2345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment