Skip to content

Strange inference failure #20596

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

Closed
jods4 opened this issue Dec 9, 2017 · 6 comments
Closed

Strange inference failure #20596

jods4 opened this issue Dec 9, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@jods4
Copy link

jods4 commented Dec 9, 2017

TypeScript Version: 2.6.2

Code

let x: { nodes: DocumentFragment, _start: Comment };
let iter: Node | null, nodes: DocumentFragment;
({ nodes, _start: iter } = x!);
while (iter) {
    let next = iter.nextSibling;
    nodes.appendChild(iter);
    iter = next;
}

Expected behavior: next is typed as Node | null.

Actual behavior: next is typed as any, which raises an error under noImplicitAny.

Comment: This is really strange because tooltips indicate that at this point iter is narrowed to Node and iter.nextSibling is of course Node | null.
So it makes no sense that let next = iter.nextSibling isn't type as such.

@jcalz
Copy link
Contributor

jcalz commented Dec 11, 2017

Essentially a duplicate of #1146 ?

@jods4
Copy link
Author

jods4 commented Dec 11, 2017

@jcalz There is a similarity but the reproductions look a bit different (loop vs super usage).
From outside it's not obvious that they are the same bug.
If someone with internal knowledge can confirm it's the same bug, feel free to close this one!

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 10, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@jods4
Copy link
Author

jods4 commented Jan 26, 2018

@mhegazy This was closed as a duplicate of #1146, which it might be, but that issue was closed without resolution...

Is the repro above really not fixable?
Tooltips in VS correctly say iter is Node, so there's no reason that let next = iter.nextSibling isn't typed properly, is there?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 29, 2018

This is a design limitation of the current compiler implementation. The type of next in the loop depends on the compiler understanding of what the type of iter is, and that in terns depend on type of next. the compiler resolves cycles like such by using any. in theory, yes, the compiler could keep iterating on both types untill a fix point is reached, but this is not how the compiler is currently designed.

@jods4
Copy link
Author

jods4 commented Feb 13, 2018

@mhegazy OK

I'm still confused by the VS tooltip indicating iter is Node in iter.nextSibling. Where does that information come from? If it's incorrect (compiler sees iter as any) does it imply there's a bug in Typescript tooling inside VS?

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants