Skip to content

fix(language-service): show warning when type inference is suboptimal - #41072

Closed
alxhub wants to merge 1 commit into
angular:masterfrom
alxhub:ngtsc/ls/info-diag-let-var
Closed

alxhub wants to merge 1 commit into
angular:masterfrom
alxhub:ngtsc/ls/info-diag-let-var

Conversation

@alxhub

@alxhub alxhub commented Mar 3, 2021

Copy link
Copy Markdown
Member

The Ivy Language Service uses the compiler's template type-checking engine,
which honors the configuration in the user's tsconfig.json. We recommend
that users upgrade to strictTemplates mode in their projects to take
advantage of the best possible type inference, and thus to have the best
experience in Language Service.

If a project is not using strictTemplates, then the compiler will not
leverage certain type inference options it has. One case where this is very
noticeable is the inference of let- variables for structural directives that
provide a template context guard (such as NgFor). Without strictTemplates,
these guards will not be applied and such variables will be inferred as
'any', degrading the user experience within Language Service.

This is working as designed, since the Language Service should reflect
types exactly as the compiler sees them. However, the View Engine Language
Service used its own type system that would infer these types even when
the compiler did not. As a result, it's confusing to some users why the
Ivy Language Service has "worse" type inference.

To address this confusion, this commit implements a warning diagnostic which
is shown in the Language Service for variables which could have been
narrowed via a context guard, but the type checking configuration didn't
allow it. This should make the reason why variables receive the 'any' type
as well as the action needed to improve the typings much more obvious,
improving the Language Service experience.

Fixes angular/vscode-ng-language-service#1155
Closes #41042

@google-cla google-cla Bot added the cla: yes label Mar 3, 2021
@pullapprove
pullapprove Bot requested review from josephperrott and kyliau March 3, 2021 23:58
@alxhub
alxhub force-pushed the ngtsc/ls/info-diag-let-var branch from e07b382 to eda0dec Compare March 4, 2021 02:28

@atscott atscott left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Awesome!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if you think we might use this flag/error code for other sub-optimal type inferences in the future. The docs are specific to context guards, but the names for the flag here and the error code seem more generic.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think in the future we might expand it to include other types (pun intended) of inference warnings.

@zarend zarend added the area: compiler Issues related to `ngc`, Angular's template compiler label Mar 4, 2021
@ngbot ngbot Bot added this to the Backlog milestone Mar 4, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a Suggestion, because Warning shows up in the "Problem" panel and usually indicates something is wrong with the code.

Here's a comparison of the UI for each DiagnosticCategory. As you can see, only suggestions do not up in the Problem panel.

Screen Shot 2021-03-04 at 1 53 48 PM

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@alxhub
alxhub force-pushed the ngtsc/ls/info-diag-let-var branch 2 times, most recently from 5951dc9 to 971411d Compare March 8, 2021 19:11
@alxhub alxhub added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Mar 8, 2021

@josephperrott josephperrott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AndrewKushnir AndrewKushnir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

@pullapprove
pullapprove Bot requested a review from jessicajaniuk March 8, 2021 19:18
@AndrewKushnir AndrewKushnir added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Mar 8, 2021
@AndrewKushnir

Copy link
Copy Markdown
Contributor

@alxhub FYI, I'm adding the "cleanup" label for now, since some errors on CI look legit.

@jessicajaniuk jessicajaniuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🍪

Reviewed-for: public-api

@pullapprove
pullapprove Bot requested a review from IgorMinar March 8, 2021 21:02
@petebacondarwin

Copy link
Copy Markdown
Contributor

There appears to be a compilation error:

packages/language-service/ivy/test/diagnostic_spec.ts:274:50 - error TS2339: Property 'WARN_SUBOPTIMAL_TYPE_INFERENCE' does not exist on type 'typeof ErrorCode'.

274     expect(diag.code).toBe(ngErrorCode(ErrorCode.WARN_SUBOPTIMAL_TYPE_INFERENCE));
                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@jessicajaniuk jessicajaniuk added state: blocked and removed action: merge The PR is ready for merge by the caretaker labels Mar 11, 2021
@alxhub
alxhub force-pushed the ngtsc/ls/info-diag-let-var branch from 971411d to ef646d5 Compare March 17, 2021 23:06

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

The Ivy Language Service uses the compiler's template type-checking engine,
which honors the configuration in the user's tsconfig.json. We recommend
that users upgrade to `strictTemplates` mode in their projects to take
advantage of the best possible type inference, and thus to have the best
experience in Language Service.

If a project is not using `strictTemplates`, then the compiler will not
leverage certain type inference options it has. One case where this is very
noticeable is the inference of let- variables for structural directives that
provide a template context guard (such as NgFor). Without `strictTemplates`,
these guards will not be applied and such variables will be inferred as
'any', degrading the user experience within Language Service.

This is working as designed, since the Language Service _should_ reflect
types exactly as the compiler sees them. However, the View Engine Language
Service used its own type system that _would_ infer these types even when
the compiler did not. As a result, it's confusing to some users why the
Ivy Language Service has "worse" type inference.

To address this confusion, this commit implements a suggestion diagnostic
which is shown in the Language Service for variables which could have been
narrowed via a context guard, but the type checking configuration didn't
allow it. This should make the reason why variables receive the 'any' type
as well as the action needed to improve the typings much more obvious,
improving the Language Service experience.

Fixes angular/vscode-ng-language-service#1155
Closes angular#41042
@alxhub
alxhub force-pushed the ngtsc/ls/info-diag-let-var branch from ef646d5 to 6b7f8f4 Compare March 19, 2021 17:30
@alxhub alxhub added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews state: blocked labels Mar 20, 2021
josephperrott pushed a commit that referenced this pull request Mar 23, 2021
…mal (#41072)

The Ivy Language Service uses the compiler's template type-checking engine,
which honors the configuration in the user's tsconfig.json. We recommend
that users upgrade to `strictTemplates` mode in their projects to take
advantage of the best possible type inference, and thus to have the best
experience in Language Service.

If a project is not using `strictTemplates`, then the compiler will not
leverage certain type inference options it has. One case where this is very
noticeable is the inference of let- variables for structural directives that
provide a template context guard (such as NgFor). Without `strictTemplates`,
these guards will not be applied and such variables will be inferred as
'any', degrading the user experience within Language Service.

This is working as designed, since the Language Service _should_ reflect
types exactly as the compiler sees them. However, the View Engine Language
Service used its own type system that _would_ infer these types even when
the compiler did not. As a result, it's confusing to some users why the
Ivy Language Service has "worse" type inference.

To address this confusion, this commit implements a suggestion diagnostic
which is shown in the Language Service for variables which could have been
narrowed via a context guard, but the type checking configuration didn't
allow it. This should make the reason why variables receive the 'any' type
as well as the action needed to improve the typings much more obvious,
improving the Language Service experience.

Fixes angular/vscode-ng-language-service#1155
Closes #41042

PR Close #41072
TeriGlover pushed a commit to TeriGlover/angular that referenced this pull request Apr 5, 2021
…mal (angular#41072)

The Ivy Language Service uses the compiler's template type-checking engine,
which honors the configuration in the user's tsconfig.json. We recommend
that users upgrade to `strictTemplates` mode in their projects to take
advantage of the best possible type inference, and thus to have the best
experience in Language Service.

If a project is not using `strictTemplates`, then the compiler will not
leverage certain type inference options it has. One case where this is very
noticeable is the inference of let- variables for structural directives that
provide a template context guard (such as NgFor). Without `strictTemplates`,
these guards will not be applied and such variables will be inferred as
'any', degrading the user experience within Language Service.

This is working as designed, since the Language Service _should_ reflect
types exactly as the compiler sees them. However, the View Engine Language
Service used its own type system that _would_ infer these types even when
the compiler did not. As a result, it's confusing to some users why the
Ivy Language Service has "worse" type inference.

To address this confusion, this commit implements a suggestion diagnostic
which is shown in the Language Service for variables which could have been
narrowed via a context guard, but the type checking configuration didn't
allow it. This should make the reason why variables receive the 'any' type
as well as the action needed to improve the typings much more obvious,
improving the Language Service experience.

Fixes angular/vscode-ng-language-service#1155
Closes angular#41042

PR Close angular#41072
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler cla: yes target: patch This PR is targeted for the next patch release

Projects

None yet

9 participants