by munchor · 7y ago · 0 comments
``` function assert(condition: boolean, message: string) { if (!condition) { throw new Error(message); } }
assert(x !== null, "x can't be null");
// Flow understands that x can't be null here because `invariant` throws an error otherwise, but TS doesn't ```
However, TypeScript couldn't understand that `x` can't be null after that call since it doesn't understand what `assert` does. For that reason, we had to write a codemod that replaced all of our `assert` calls with an if condition that threw an error inline.
The new `asserts` syntax is nice (it would be even better if TS could do this automatically like Flow though), and will be a much welcome update for us.
What are they good for? Maybe there is a use case I am not getting, but this
function yell(str) {
assert(typeof str === "string");
return str.toUppercase();
// ~~~~~~~~~~~
// error: Property 'toUppercase' does not exist on type 'string'.
// Did you mean 'toUpperCase'?
}
function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new AssertionError(msg)
}
}
Seems like the equivalent to function yell(str : String) {
return str.toUppercase();
// ~~~~~~~~~~~
// error: Property 'toUppercase' does not exist on type 'string'.
// Did you mean 'toUpperCase'?
}
But the latter has much less code and is clearer too.What am I missing?
Why should the type system have anything to do with something like this?
function something(num : number) {
assertIsInRange(num);
return num + 1;
}
function assertIsInRange(num: number, msg?: string): asserts num {
if (num < 50 || num > 100) {
throw new Error('not in range');
}
}
something(50);
How is `asserts num` helping here?The fact that it’s a built in feature of the language also can sometimes helps the compiler infer what’s the properties of code after it. But theoretically , yes you could very well code some equivalent testing function yourself, except for the compiler inference part it would probably be equivalent.
function assertIsString(x: number | string): asserts x is string {
}
and TS will just ignore the fact that you didn’t check anything. One shouldn’t have to introduce a giant hole in the type system just to be able to encapsulate checks that TS should be capable of understanding.I filed this as a bug to see if the developers agree: https://github.com/microsoft/TypeScript/issues/33743
function test(x: number | string): void {
x.toUpperCase();
}
is an error and function test(x: number | string): void {
if (typeof x === "number")
throw new Error();
x.toUpperCase();
}
is not. So it should just as easily be able to understand that function assertIsString(x: number | string): asserts x is string {
}
is an error and function assertIsString(x: number | string): asserts x is string {
if (typeof x === "number")
throw new Error();
}
is not.That would imply that the same function signature could produce different type checking results at the call site. That is something I find extremely frustrating as you can no longer reason about type checking locally.