Skip to content

Unable to resolve a const variable type with nested ref with noImplicitAny enabled #17386

@takamori

Description

@takamori

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

function hello() {
  let lastThing = null;
  for (let i = 0; i < 2; i++) {
    const thing = new Thing(!lastThing ? null : lastThing);
    lastThing = thing;
  }
}

class Thing  {
  constructor(public readonly prior: Thing|null) {
  }
}

Expected behavior:
Since thing is being defined directly, the compiler should still intuit that it's of type Thing when noImplicitAny is enabled.

Actual behavior:
The compiler doesn't like the nested data reference via the loop and lastThing reference and says thing will be "any" and I have to explicitly define thing as type Thing:

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

This is a simplified version of the actual code; the existence of a loop and the reference to the prior instance are critical to the actual usage. Repros with the TS playground with noImplicitAny enabled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions