Infer correct type when `trackBy` is used in `ngFor`

fix(common): infer correct type when `trackBy` is used in `ngFor` (#41995)
Infer correct type when `trackBy` is used in `ngFor`fixinfer correct type trackby used ngfor

Committed by JoostK

commit message
fix(common): infer correct type when `trackBy` is used in `ngFor` (#41995)

When a `trackBy` function is used that accepts a supertype of the iterated
array's type, the loop variable would undesirably be inferred as the supertype
instead of the array's item type. This commit adds an inferred type parameter
to `TrackByFunction` to allow an extra degree of freedom, enabling the
loop value to be inferred as the most narrow type.
 
Fixes #40125
 
PR Close #41995

Editor assessment

An exemplar of explaining a type-level fix. The subject states the defect - wrong inference when trackBy is used in ngFor - and the body teaches the mechanism: a trackBy function accepting a supertype pulled the loop variable to that supertype, and an inferred type parameter on TrackByFunction restores the narrow item type. Problem, cause, and remedy with the issue linked and clean hygiene throughout. The supertype-inference explanation is exact and the remedy is named at the type level, which is the complete technical story.

Browse categories

More fix examples · Back to search