Skip to content

Type inference changed for callbacks with optional parametersΒ #55394

Closed
@h-joo

Description

@h-joo

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions