Skip to content

fix(compiler-cli): infer correct generic types for directives when so… #57724

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JoostK
Copy link
Member

@JoostK JoostK commented Sep 8, 2024

…me inputs are omitted

The template type-checker could inadvertently end up inferring any for a directive's generic type argument, effectively disabling all type checking of values of that generic type. This happened when the directive reflects the generic type in multiple inputs, some of which were not bound from the parent template. Since omitted inputs were passed into the directive's type constructor using the 0 as any expression, TypeScript sees the input of type any as inference source of the generic type and locks that in.

The initial remediation idea was to remove omitted inputs from the type-constructor entirely, changing its type from Pick<Dir, 'a'|'...'> to Partial<Pick<Dir, 'a'|'...'>> to make all input bindings optional. This introduces issues where the undefined type that is introduced affects type inference, causing test failures.

Instead, omitted inputs are now passed into the type constructor as never type, which is TypeScript's bottom type such that no inference candidates are introduced.

Fixes #57644


This change is likely (too) breaking, as the inference of the any type used to effectively disable type-checking of any (pun not intended) expressions that reflect a generic type of a directive with missing inputs. MatCalendar is one such directive (the one reported in #57644) that has many inputs that reflect the generic type, where it is typical that not all those inputs are actually bound.

A g3 global presubmit should help determine the impact of the breakage, which may require us to introduce this behind a compiler option.

@JoostK JoostK added breaking changes compiler: template type-checking action: global presubmit The PR is in need of a google3 global presubmit area: compiler Issues related to `ngc`, Angular's template compiler labels Sep 8, 2024
@ngbot ngbot bot added this to the Backlog milestone Sep 8, 2024
@JoostK JoostK force-pushed the compiler/ttc/partial-type-ctors branch from a45bf44 to 4b35c7e Compare September 20, 2024 19:28
@angular-robot angular-robot bot removed the area: compiler Issues related to `ngc`, Angular's template compiler label Sep 20, 2024
@ngbot ngbot bot removed this from the Backlog milestone Sep 20, 2024
@JoostK JoostK force-pushed the compiler/ttc/partial-type-ctors branch from 4b35c7e to 7c4614d Compare September 22, 2024 17:03
…me inputs are omitted

The template type-checker could inadvertently end up inferring `any` for a directive's generic type
argument, effectively disabling all type checking of values of that generic type. This happened
when the directive reflects the generic type in multiple inputs, some of which were not bound from
the parent template. Since omitted inputs were passed into the directive's type constructor using
the `0 as any` expression, TypeScript sees the input of type `any` as inference source of the
generic type and locks that in.

The initial remediation idea was to remove omitted inputs from the type-constructor entirely,
changing its type from `Pick<Dir, 'a'|'...'>` to `Partial<Pick<Dir, 'a'|'...'>>` to make all input
bindings optional. This introduces issues where the `undefined` type that is introduced affects
type inference, causing test failures.

Instead, omitted inputs are now passed into the type constructor as `never` type, which is TypeScript's
bottom type such that no inference candidates are introduced.

Fixes angular#57644
@JoostK JoostK force-pushed the compiler/ttc/partial-type-ctors branch from 7c4614d to 9f60e5b Compare September 29, 2024 20:40
@thePunderWoman
Copy link
Contributor

@JoostK Are you still working on this, or can we close it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic types of components/directives are inferred as any when some corresponding inputs are omitted
2 participants