Skip to content

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

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
sparecycles opened this issue Aug 9, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@sparecycles
Copy link
Contributor

sparecycles commented Aug 9, 2024

πŸ”Ž 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!

@IllusionMH
Copy link
Contributor

Looks like duplicate of #59497

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 9, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
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