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

Type inference changed for callbacks with optional parameters #55394

Closed
h-joo opened this issue Aug 16, 2023 · 2 comments Β· Fixed by #55397
Closed

Type inference changed for callbacks with optional parameters #55394

h-joo opened this issue Aug 16, 2023 · 2 comments Β· Fixed by #55397
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@h-joo
Copy link
Contributor

h-joo commented Aug 16, 2023

πŸ”Ž Search Terms

optional, inference, callback

πŸ•— Version & Regression Information

  • Behaviour changed between versions 5.0 and 5.1

⏯ Playground Link

https://www.typescriptlang.org/play?jsx=0&module=1&ts=5.1.3#code/KYDwDg9gTgLgBAMwK4DsDGMCWEWMwGxmCgB4AVAPgAowpgATTNAQyIC44qA3Z-JYDmQA0cTCnqgOKJAFsARsQCUcALwU4ciBHzBmKRYLgBvAL4AoM2hwBneM1V5CxKjQjXgAfimyFUZWrgYKH5FAG4LKxRbDQcEAiIoF0h3LzhpeWIRAH1U9N9-dSCQ0KA

πŸ’» Code

export function filter<T>(predicate: (value: T, index: number) => boolean): T {}
const a = filter((pose?: number) => true);

πŸ™ Actual behavior

a gets inferred as number

πŸ™‚ Expected behavior

a gets inferred as number|undefined (this is the case in 5.0.4)

I'm not certain if this change is caused by a bug or intentional, but I couldn't find anything from the announcement at least that made this change, so rather asking for clarification on whether this is an intended behavior

Additional information about the issue

The original definition of filter comes from rxjs (simplified in the example for reporting purpose) : https://github.com/ReactiveX/rxjs/blob/2947583bb33e97f3db9e6d9f6cea70c62a173060/src/internal/operators/filter.ts#L6

@Andarist
Copy link
Contributor

Changed in 5.1.0-dev.20230317, this is what has been added in this version: diff. So likely the difference comes from #52609 .

I think this is a bug. Perhaps it was always there in inferFromAnnotatedParameters or maybe there is a specific reason why optional parameters behave differently there. I see that the inference source that is used is just getEffectiveTypeAnnotationNode(declaration) there and that the optional marker is ignored. I'll investigate this later more thoroughly.

@fatcerberus
Copy link

fatcerberus commented Aug 16, 2023

It’s kind of a gray area, but the current behavior feels more intuitive fwiw - the optional marker suggests the intent is that the callback either receives a number or else nothing at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants