Skip to content

Inconsistency between destructuring and object access in filter-narrowing.Β #59581

Closed as not planned
@sparecycles

Description

@sparecycles

πŸ”Ž Search Terms

filter narrow destructuring

πŸ•— Version & Regression Information

This is recent functionality, in 5.5, I think? (type narrowing via array filter functions).

⏯ Playground Link

https://www.typescriptlang.org/play/?target=7&jsx=0&ts=5.5.4#code/KYDwDg9gTgLgBAbwL4G4BQaCGBnAngOwGM4AzAVyJgEsJ84QAKASkTTnbimBjKjoHIAFsAA2IiP3RIMDTAHdMVeAAUoEALZVswAHSYxAZW4wRwACYMA2oyYBdJkzZwdJKiJjAoDBthYBeAD44bB1sGEwebDg-GLgAInIRVzFzOMd2HXVMMG8EOAA3fTJgOCR-IMKRYqZ0NFkFJThVDS1dfREjGBNzKxt7dOdkjy8GPLCIsiiy6KDxyOjYhLIkt1MzNKdM7NyCopLpwN2q4BqMM-O0IA

πŸ’» Code

async function x() {
    return 'hello';
}

(await Promise.allSettled([x()]))
  .filter((s) => s.status === "fulfilled") // narrows type to PromiseFulfilledResult<string>[]
  .map(({ value }) => value);

(await Promise.allSettled([x()]))
  .filter(({ status }) => status === "fulfilled") // does not narrow type
  .map(({ value }) => value); // unsure that value exists

πŸ™ Actual behavior

image

πŸ™‚ Expected behavior

Expect the destructured version to narrow the element type as well.

Additional information about the issue

I found #28311 when searching for existing bugs, looks like it can be closed by the existing 5.5 features, good work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions