Skip to content

docs: make all examples compatible with strict mode - #41999

Closed
crisbeto wants to merge 3 commits into
angular:masterfrom
crisbeto:examples-strict-mode
Closed

crisbeto wants to merge 3 commits into
angular:masterfrom
crisbeto:examples-strict-mode

Conversation

@crisbeto

@crisbeto crisbeto commented May 8, 2021 •

Copy link
Copy Markdown
Member

Turns on the strict compiler flag and resolves the compilation errors in the various AIO examples.

Closes #42107

@google-cla google-cla Bot added the cla: yes label May 8, 2021
@crisbeto
crisbeto force-pushed the examples-strict-mode branch 3 times, most recently from e3f08d7 to 55aee27 Compare May 8, 2021 10:04
@crisbeto
crisbeto force-pushed the examples-strict-mode branch 6 times, most recently from 17dac6f to 35cf94d Compare May 8, 2021 14:02
@crisbeto
crisbeto marked this pull request as ready for review May 8, 2021 14:20
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer comp: examples target: rc This PR is targeted for the next release-candidate labels May 8, 2021
@ngbot ngbot Bot modified the milestone: Backlog May 8, 2021
@crisbeto
crisbeto requested a review from petebacondarwin May 8, 2021 14:22

@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.

Brilliant work @crisbeto - I made a lot of comments but then there were a LOT of changes.

The main comment appears to be that you have a stylistic approach to asserting that something is not null by casting it:

function foo(): string|null {}

// I know foo will be non-null
const v: string = foo() as string;

I don't like to use this approach for two reasons:

  1. It is more verbose.
  2. We already know what the non-null/undefined type is, so this is making the code more brittle to changes to the type, since the type will no longer flow naturally and have to be updated wherever these casts are.

I prefer to simply assert that they are well defined.

function foo(): string|null {}

// I know foo will be non-null
const v: string = foo()!;

Comment on lines 15 to 22

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 the only change needed here is form!:

Suggested change
@Input() questions: QuestionBase<string>[] | null = [];
form!: FormGroup;
payLoad = '';
constructor(private qcs: QuestionControlService) { }
constructor(private qcs: QuestionControlService) {}
ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);
this.form = this.qcs.toFormGroup(this.questions as QuestionBase<string>[]);
@Input() questions: QuestionBase<string>[] = [];
form!: FormGroup;
payLoad = '';
constructor(private qcs: QuestionControlService) {}
ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);

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.

Hmm, I left a comment on this yesterday but it got lost somehow 🤔. The reason I changed questions to | null is because the example passes them in with an async pipe in one of the templates. The async pipe starts off with a null value.

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.

In which case we should not be casting it to QuestionBase<string>[] on line 22, right?

@crisbeto

crisbeto commented May 8, 2021

Copy link
Copy Markdown
Member Author

Regarding the non-null assertions: that's actually what I prefer as well, but there's a lint rule that doesn't allow them. I believe it's one that is turned on by default in the CLI.

@petebacondarwin

petebacondarwin commented May 8, 2021 •

Copy link
Copy Markdown
Contributor

Regarding the non-null assertions: that's actually what I prefer as well, but there's a lint rule that doesn't allow them. I believe it's one that is turned on by default in the CLI.

What 😱 ??? That is a bad lint rule.

Since tslint is deprecated in v12, we should not be using it for our examples anyway :-)

So I would recommend deleting the tslint.json file from the boillerplate and removing the lint rule from the angular.json. See https://github.com/cexbrayat/angular-cli-diff/compare/11.2.9..12.0.0-rc.2#diff-35cddbe68960b288daa0aeb84cc609451029d160e3524e0d3ad17061cc20eeaeL99

@jelbourn jelbourn 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 May 15, 2021 13:22

@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

@AndrewKushnir

Copy link
Copy Markdown
Contributor

@crisbeto it looks like this PR has all the necessary approvals now :)

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 15, 2021
@JoostK JoostK linked an issue May 16, 2021 that may be closed by this pull request
@atscott atscott closed this in e86a1d3 May 17, 2021
atscott pushed a commit that referenced this pull request May 17, 2021
Turns on the `strict` compiler flag and resolves the compilation errors in the various AIO examples.

PR Close #41999
gkalpak added a commit to gkalpak/angular that referenced this pull request May 19, 2021
This commit aligns the code in `product-alerts.component.ts` with
[product-alerts.component.1.ts][1], since both files are supposed to
represent the same component in different points in time. It also makes
the necessary changes in the respective templates.

This is a follow-up to angular#41999.

[1]: https://github.com/angular/angular/blob/e86a1d3441b30776e28c0216f00610d6787de297/aio/content/examples/getting-started/src/app/product-alerts/product-alerts.component.1.ts#L18
zarend pushed a commit that referenced this pull request May 24, 2021
This commit aligns the code in `product-alerts.component.ts` with
[product-alerts.component.1.ts][1], since both files are supposed to
represent the same component in different points in time. It also makes
the necessary changes in the respective templates.

This is a follow-up to #41999.

[1]: https://github.com/angular/angular/blob/e86a1d3441b30776e28c0216f00610d6787de297/aio/content/examples/getting-started/src/app/product-alerts/product-alerts.component.1.ts#L18

PR Close #42155
zarend pushed a commit that referenced this pull request May 24, 2021
This commit aligns the code in `product-alerts.component.ts` with
[product-alerts.component.1.ts][1], since both files are supposed to
represent the same component in different points in time. It also makes
the necessary changes in the respective templates.

This is a follow-up to #41999.

[1]: https://github.com/angular/angular/blob/e86a1d3441b30776e28c0216f00610d6787de297/aio/content/examples/getting-started/src/app/product-alerts/product-alerts.component.1.ts#L18

PR Close #42155
umairhm pushed a commit to umairhm/angular that referenced this pull request May 24, 2021
…2155)

This commit aligns the code in `product-alerts.component.ts` with
[product-alerts.component.1.ts][1], since both files are supposed to
represent the same component in different points in time. It also makes
the necessary changes in the respective templates.

This is a follow-up to angular#41999.

[1]: https://github.com/angular/angular/blob/e86a1d3441b30776e28c0216f00610d6787de297/aio/content/examples/getting-started/src/app/product-alerts/product-alerts.component.1.ts#L18

PR Close angular#42155
iRealNirmal pushed a commit to iRealNirmal/angular that referenced this pull request Jun 4, 2021
…2155)

This commit aligns the code in `product-alerts.component.ts` with
[product-alerts.component.1.ts][1], since both files are supposed to
represent the same component in different points in time. It also makes
the necessary changes in the respective templates.

This is a follow-up to angular#41999.

[1]: https://github.com/angular/angular/blob/e86a1d3441b30776e28c0216f00610d6787de297/aio/content/examples/getting-started/src/app/product-alerts/product-alerts.component.1.ts#L18

PR Close angular#42155
@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 Jun 17, 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 cla: yes target: patch This PR is targeted for the next patch release

Projects

None yet

5 participants