Skip to content

returning from a for loop breaks narrowing #36088

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
SLaks opened this issue Jan 8, 2020 · 2 comments
Closed

returning from a for loop breaks narrowing #36088

SLaks opened this issue Jan 8, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@SLaks
Copy link

SLaks commented Jan 8, 2020

TypeScript Version: 3.8.0-dev.20200107 (nightly on Playground

Search Terms: for return loop

Code

function test() {
  for (var x: string | number = 'a'; typeof x === 'string'; x = x.toUpperCase()) {
    return;
  }
}

Expected behavior:
No error, just as if return; were not present

Actual behavior:

The terminating condition fails to narrow the type for the increment clause:

Property 'toUpperCase' does not exist on type 'string | number'.

I suspect this happens because exiting the loop early (especially with break; means that the narrowing doesn't apply after the loop body.
However, that's never true for the increment clause.

Playground Link: link

Related Issues: None

@RyanCavanaugh
Copy link
Member

See #26914

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 8, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants