Skip to content

Extract to inner function drops narrowed type  #49576

@mjbvz

Description

@mjbvz

Bug Report

🔎 Search Terms

  • refactor
  • refactoring

🕗 Version & Regression Information

4.8.0-dev.20220616, not a regression

💻 Code

function doThing(x: number | string) { 
    if (typeof x === 'string') {
        x.charCodeAt(0); // Run extract to inner function here
    }
}

🙁 Actual behavior

The resulting code is invalid:

function doThing(x: number | string) {
    if (typeof x === 'string') {
        newFunction();
    }

    function newFunction() {
        x.charCodeAt(0); // Error here: x is of type number | string
    }
}

🙂 Expected behavior

We either don't offer the refactoring in this case or we somehow pass the narrowed type to the function

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions