-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
TypeScript Version: 3.7.0-dev.20191016
Search Terms: assertion signatures, asserts, asserts is type
Code
function isString1(val: any): asserts val is string {
if (typeof val !== "string") throw "Nope"
}
const isString2 = (val: any): asserts val is string => {
if (typeof val !== "string") throw "Nope"
}
const x: any = "3";
isString1(x);
isString2(x);
x.toUpperCase();
Expected behavior:
Compiles, infers that x
is a string, both when using only isString1
or isString2
Actual behavior:
Compile error on isString2
:
Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
Playground Link: 1⃣ playground
Related Issues: #33743
Context
In principle the above problem is easy to work around, however, the result of it is when type checkers are stored on objects, the above error returns as is demonstrated in 2⃣ this playground. Note how the very same function fails when it is stored in an object, unless the type is provided explicitly.
mateja176, danilofuchs, matobeno1, ibezkrovnyi, willingram and 27 more
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed