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

Typescript loses constraint of satisfies on initial value/accumulator for Array.prototype.reduce #52526

Closed
DesignByOnyx opened this issue Jan 31, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@DesignByOnyx
Copy link

🔎 Search Terms

  • satisfies
  • reducer
  • nested array

🕗 Version & Regression Information

  • v4.9.4 - I assume this affects >=4.9.0 when the satisfies operator was introduced

💻 Repo

type ParseResult = { errors: Error[] }

'foob@r'.split(' ').reduce((acc, part) => {
    if (/^\w+$/.test(part)) {
        acc.errors.push(new Error('Not a valid term'));
        //              ^^^^ Argument of type 'Error' is not assignable to parameter of type 'never'.
    }
    return acc;
}, { errors: [] } satisfies ParseResult)

Playground

🙁 Actual behavior

The accumulator loses some of that information from the satisfies constraint, especially as it pertains to arrays. Typescript incorrectly thinks the errors array is type never[].

🙂 Expected behavior

The errors array should have the same type as asserted with the satisfies operator. As you can see in the playground, you can use the as operator, but you lose all of the benefits that satisfies provides. In my head, the satisfies operator is the same as as, but with actual type checking happening on the operands.

@DesignByOnyx DesignByOnyx added the Duplicate An existing issue was already created label Jan 31, 2023
@DesignByOnyx
Copy link
Author

DesignByOnyx commented Jan 31, 2023

I had too narrow of a search before I opened this. I see the duplicate now that I have expanded my search (#51853). Apologies for the noise.

@fatcerberus
Copy link

@DesignByOnyx Your issue got auto-labeled as a dupe because you probably used the template for 9998 dupes (“types not correct in/with callback”, which has to do with type guards and is totally unrelated), but it’s good that you found the actual dupe anyway. 😄

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

2 participants