Skip to content

Type narrowing fails when there is a break/return in loop #40393

@xuld

Description

@xuld

TypeScript Version: 4.0.2

Search Terms:
flow type narrowing loop

Code

for (let p: number | string = 1; p; p = p + 1) {
    // Just make sure for iterator is reachable
    if (p) {
        continue
    }
    // Actually unreachable here
    // Comment this line out, everything works correctly
	break
}

Expected behavior:
No errors. p is typed number in p = p + 1

Actual behavior:
p is number | string

Playground Link: https://www.typescriptlang.org/zh/play?target=1#code/GYewTgBAFANgpgFwgBwFwQHYFcC2AjOSAHwgGcEwBLDAcwgF4IBGAbhTeQZQgGpmBKCAG8AsACgIkiJWDRkg0RKnKAxiAwJqWOOOUBfXVID0RiAEEVCLAEMYMAJ4QsGMHGsqAFtbzwIHwjpiAJB4rtYA1uJ6QA

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysis

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions