Skip to content

Incorrect type inference inside stateful loop #39183

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
bcuccioli opened this issue Jun 21, 2020 · 1 comment
Closed

Incorrect type inference inside stateful loop #39183

bcuccioli opened this issue Jun 21, 2020 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@bcuccioli
Copy link

TypeScript Version: 'Nightly' in playground

Search Terms: stateful loop inference any

Code

async function getPage(cursor: string | null) {
  return {
    results: <string[]>[],
    cursor: cursor + '_',
  };
}

async function paginate() {
  let cursor: string | null = null;

  while (true) {
    const page = await getPage(cursor);
    cursor = page.cursor;
  }
}

Expected behavior: No error, page type inferred specifically.

Actual behavior: 'page' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)

Playground Link: https://www.typescriptlang.org/play/?ts=4.0.0-dev.20200620#code/IYZwngdgxgBAZgV2gFwJYHsIwOYFNkAKweAFFAgE4joUBcMIyFqE2MAPjBAgDY8CUMAN4AoGDAr5KWUePGSQvZCHoAeRs1YBtALoA+XQBoxc8lRr0z1CjADUMAOQB9B8fEBfANwj3IkaEhYRBQMLAAHYhZgZFwSQVkYHnwYKwsGJhY2Tm4+GABeLl4ebxMAdwALVCSYEiYEXHiTcShMRhgIvHyYYFLgVGQcfCJSVIp+bzkUymsujtwAOlGJmF9fIA

Is this unexpected, or a known limitation?

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 22, 2020
@RyanCavanaugh
Copy link
Member

This is a circularity because the return type of getPage could depend on the type of its parameter, so the loop creates a circularity: getPage(cursor) depends on cursor's possible values which depends on the type of page.cursor which depends on the type of getPage(cursor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants