Skip to content
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

Undefined not narrowed out of array pop/shift return type despite check #47846

Closed
wbt opened this issue Feb 11, 2022 · 2 comments
Closed

Undefined not narrowed out of array pop/shift return type despite check #47846

wbt opened this issue Feb 11, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@wbt
Copy link

wbt commented Feb 11, 2022

Bug Report

🔎 Search Terms

array pop shift undefined length

🕗 Version & Regression Information

Observed in 3.7.5 - 4.5.5 (latest) + nightly as of issue filing.
Prior versions claim an error but aren't as specific; there's no reason to believe this is a regression.

⏯ Playground Link

Playground link with relevant code

💻 Code

export class MyClass {   
    typescriptCopyExample(): Array<string> {
        const stringsToCopy = ['a', 'b', 'c'];
        let result: Array<string> = [];
        while(stringsToCopy.length>0) {
            result.push(stringsToCopy.pop()); // Error: 'string | undefined' is not assignable to parameter of type 'string'
        }
        return result;
    }   
}

🙁 Actual behavior

An error on the flagged line that the result of pop() could be undefined, which is what happens when the array is empty, even inside a conditional check that this is not the case.

🙂 Expected behavior

Inside the conditional check, stringsToCopy is narrowed to an array of length > 0. The return type of pop() and shift() operations are not flagged as possibly undefined.

@MartinJohns
Copy link
Contributor

Duplicate of #30406.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 11, 2022
@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

4 participants