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

Type guard fails to narrow type in specific situations with (indirectly) recursive types #37631

Open
marijnh opened this issue Mar 26, 2020 · 2 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@marijnh
Copy link

marijnh commented Mar 26, 2020

TypeScript Version: 3.8.3

Search Terms: guard recursive

Code

(This is very weird code, result of reducing a real-world situation to the minimum complexity necessary to reduce this.)

class A<Value> {
  prop!: (x: Value) => void
}
class B<Value> {
  prop!: A<Value>
  is<T>(): this is B<T> { return true }
}

let b: B<any> = null as any
if (b.is<string>()) {
  // b will still be of type B<any> here
}

Expected behavior:

The conditional would, due to the type guard on the is method, narrow the type of b to B<string>.

Actual behavior:

b remains of type B<any>. But, when the line that defines A.prop is commented out, it does correctly get narrowed to B<string>. If its type is changed to just Value, or some function type that doesn't involve Value, the problem also goes away.

Playground Link: here

Related Issues: none that I found

@RyanCavanaugh
Copy link
Member

This works as expected for me at your Playground link. Is there more configuration needed to reproduce the problem?
image

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Mar 27, 2020
@marijnh
Copy link
Author

marijnh commented Mar 27, 2020

Apologies, I pasted a slightly incorrect playground link. Updated to this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants