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

Circular reference misdetection #32950

Closed
falsandtru opened this issue Aug 17, 2019 · 6 comments
Closed

Circular reference misdetection #32950

falsandtru opened this issue Aug 17, 2019 · 6 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@falsandtru
Copy link
Contributor

TypeScript Version: 3.4.0-dev.20190817

Search Terms:

Code

export function f<T extends HTMLOListElement | DocumentFragment>(target: T): T {
  if (target instanceof HTMLOListElement) {
    for (const el of target.querySelectorAll('a')) {
      if (el.closest('ol') !== target) continue;
    }
  }
  return target;
}

Expected behavior:
pass

Actual behavior:

const el: any
'el' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

Playground Link: http://www.typescriptlang.org/play/index.html#code/KYDwDg9gTgLgBAMwK4DsDGMCWEWIDwAqcoMwKAJgM5wASBAsgDIDyjmlMAogDbAC2ZeAB84AEQhokAlDABiUAIYBzaTAB8AChgKoS4DABccAgEojRAN4AoOHEwI4WnXviYUHBemAQHdJq3YuXlUTOGtbWwRoRzQcDmJuOB84bV19ADoARyRgKABPAGVgXgxoAEFubg0AcgVqk1DwiNt7R2L0tG4ISmAOGohuergAQgBeUZTnfVDYmTccgG4bCIBfZbXbKH0kKFxUlyWVoA

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 19, 2019
@RyanCavanaugh
Copy link
Member

🤔🤔🤔 Usually there's some sneaky dependency here, but I can't find one. We might be incorrectly thinking that the back edge of the for loop somehow influences el, but it doesn't?

Simplifiable a bit

export function f(target: HTMLOListElement | DocumentFragment) {
  if (target instanceof HTMLOListElement) {
    for (const el of target.querySelectorAll('a')) {
      if (el === target) {
      }
    }
  }
}

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 19, 2019
@falsandtru
Copy link
Contributor Author

This bug is found in writing a polyfill of :scope keyword for Edge browser.

// expect
if (target instanceof HTMLOListElement) {
  for (const el of target.querySelectorAll(':scope > li > a')) {
  }
}
// polyfill
if (target instanceof HTMLOListElement) {
  for (const el of target.querySelectorAll('a')) {
    if (el.closest('ol') !== target) continue;
  }
}

@ahejlsberg
Copy link
Member

I think what's happening here is that in order to determine the type of el we have to determine the type of target.querySelectorAll('a'), which requires us determine the control flow type of target, which could be narrowed by the expression el.closest('ol') !== target, which requires us to determine the type of el, which triggers a circularity.

@thorn0
Copy link

thorn0 commented Sep 5, 2019

Another instance (playground; reproduces in TS 3.6.2 too):

declare function f(): number | undefined;
const [a, b = a] = [10, f()];
// 'a' implicitly has type 'any' because it does not have a type annotation and 
// is referenced directly or indirectly in its own initializer.

Or is this #33191? Or are these two issues the same?

@falsandtru
Copy link
Contributor Author

Not similar to the others. Should be filed as a separated issue.

mprobst added a commit to angular/angular that referenced this issue Oct 21, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.
mprobst added a commit to mprobst/angular that referenced this issue Oct 25, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.
atscott pushed a commit to angular/angular that referenced this issue Nov 1, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.

PR Close #33294
atscott pushed a commit to angular/angular that referenced this issue Nov 1, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.

PR Close #33294
mohaxspb pushed a commit to mohaxspb/angular that referenced this issue Nov 7, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.

PR Close angular#33294
mohaxspb pushed a commit to mohaxspb/angular that referenced this issue Nov 7, 2019
In TypeScript 3.7, circularity detection misfires on the declaration of `value` here.
microsoft/TypeScript#32950

Declaring an explicit type avoids the problem.

PR Close angular#33294
@danvk
Copy link
Contributor

danvk commented Feb 28, 2024

This was fixed in d26afd7 which was part of #32695, which was released in TS 3.7 back in 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

5 participants