-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Incorrect report of Signature '(): false' must be a type predicate
since TS 5.5
#59252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Signature '(): false' must be a type predicate
since TS 5.5
This is a design limitation. Types are not inferred from the implementing interface. See #1373. |
Right, I see. Thanks for the info! I guess that part is unrelated then. |
This has changed in #57341 . This type predicates became consistent there with identifier type predicates. Notice how both of those are errors in TS 5.4: export interface Foo {
isNumber: (a: unknown) => a is number;
}
const foo1: Foo = {
isNumber: (arg) => {
return false;
},
};
const foo2: Foo = {
isNumber: (arg) => {
return typeof arg === "number";
},
}; In TS 5.5 the second one is no longer an error. Nice! The takeaway is that functions are never contextually-typed as type predicates, which could very well be by design. But to be sure we should wait for a TS team member to chime in. |
Not contextually typing functions as having type predicates is intentional, since it'd be way too easy to write an incorrect type guard without realizing that the thing you're writing is a type guard (because they're unchecked). |
I understand. How should the above JavaScript be expressed as idiomatic TypeScript then? |
Well, that's a good question |
Perhaps drop the safety net and use the LSP to make it more transparent that |
This is just a bug; const foo2 = {
isNumber(): this is { b: string } {
return true;
},
}; |
Nice, thanks for the fix! |
π Search Terms
type predicate this inference object
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.3#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgeTBgQiUwBsAeAFQD44BvAKDlbgDcKBXYAfgC441ANws2AC0wBnAGrdgACgCUgmOIRTEmwsVIUa9AGSMO8wdTgBfUZabZSU+JgBGU4CkE6SZKki4BbZzR6AF5GMVZOch5BLiQAE2B0ZGB4gBoIuElZeWVwtgK4KGAYLigkOCxyN1ECywzrJiA
π» Code
π Actual behavior
π Expected behavior
Code compiles just fine, just like it did prior to 5.5.
Additional information about the issue
It seems like it is impossible to install type predicates for
this
on POJOs. That is unexpected because the code works at runtime, and it compiled with TS 5.4. At runtime,this
simply refers to the object which the function is installed on, and it is expected that the compiler can figure this out.Note that this code does not even work when using a class as compilation fails for
with a related error:
You can, however, add the return type fot the type predicate explicitly and do
which compiles just fine, but that obviously requires us to use
which was not the original goal.
Adding the same explicit type annotation to the POJO is naturally not possible because
A 'this' type is available only in a non-static member of a class or interface.
The text was updated successfully, but these errors were encountered: