Skip to content
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

User-defined type guard method can be overridden with any non-type-guard return type #27687

Open
mattmccutchen opened this issue Oct 10, 2018 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@mattmccutchen
Copy link
Contributor

TypeScript Version: master (07966dc)

Search Terms: user-defined type guard predicate method override overridden return

Code

interface A {
    isB(): this is B;
}
interface B extends A {
    isB(): "this is nonsense";
}

Expected behavior: Error on "this is nonsense".

Actual behavior: No error.

Playground Link: link

Related Issues: none found

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 10, 2018
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Oct 10, 2018
@mattmccutchen
Copy link
Contributor Author

mattmccutchen commented Oct 10, 2018

@RyanCavanaugh I'll be happy to make a PR if you tell me the intended behavior. A this type guard function can be overridden with any function returning a boolean, or it can only be overridden with a this type guard function for the same type (as is the rule for parameter type guards and is sound)? The previous discussion I found is here.

@RyanCavanaugh
Copy link
Member

I'd be curious to see the results of both, but if I had to pick one without seeing RWC results I'd say "must be boolean or the same guarded type".

@gregolai
Copy link

gregolai commented Jan 10, 2019

I wonder if this issue is related, where a derived method (abstract/non-abstract) does not infer the correct type/interface:

Playground Link: link

There is some type checking going on. For example, if you return a number in the derived method, it correctly shows an error.

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants