Skip to content
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

Angular: Make CLI templates compatible with TS strict mode #12081

Merged
merged 2 commits into from Aug 17, 2020

Conversation

gaetanmaisse
Copy link
Member

@gaetanmaisse gaetanmaisse commented Aug 17, 2020

Issue: #12070

What I did

  • More and more devs are using TS strict mode as it's advocated by TS team. So we should generate Angular project in E2E tests with strict mode activated.
  • Fix typing errors in strict mode.

How to test

  • CI should be green, especially example-v2 for Angular.

More and more devs are using TS strict mode as it's advocated by TS team.
So generate Angular with strict mode activated.
@shilman shilman added this to the 6.0.x milestone Aug 17, 2020
@gaetanmaisse gaetanmaisse marked this pull request as ready for review August 17, 2020 13:11
@@ -37,7 +37,7 @@ export default class ButtonComponent {
* @required
*/
@Input()
label: string;
label = 'Button';
Copy link
Member

@shilman shilman Aug 17, 2020

Choose a reason for hiding this comment

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

Why can't this just be a required string? I don't know if it makes sense to have a default value?

Copy link
Member

Choose a reason for hiding this comment

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

Also, welcome back!! 🎉

Copy link
Member Author

Choose a reason for hiding this comment

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

@shilman you mean optional? I was going to make it optional but the @required 2 lines above and it scared me 😱 😄 .

If we want to keep it as "string" we need to provide a default value, otherwise, it's a string | undefined

Copy link
Member

@shilman shilman Aug 17, 2020

Choose a reason for hiding this comment

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

Why can't it be required and NOT have a default value?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because of strictPropertyInitialization (enabled in strict mode): https://devblogs.microsoft.com/typescript/announcing-typescript-2-7/#stricter-class-property-checks

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for that. TypeScript 🤦

Choose a reason for hiding this comment

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

Well, this actually makes sense. The Angular compiler does not force you to set required Inputs in your template. Thus, even if you claim that an Input is required, at runtime it might be undefined if you don't assign a default value.

On the other hand, even if the Angular compiler enforced this, how would the framework-agnostic Typescript compiler know this?

So, this problem arises from the way of how components are built upon Typescript classes.

Copy link
Contributor

@tooppaaa tooppaaa left a comment

Choose a reason for hiding this comment

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

✔️ Welcome back ;)

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

💯

@shilman shilman merged commit 57e614b into next Aug 17, 2020
@gaetanmaisse gaetanmaisse deleted the fix-12070-angular-cli-story-templates branch November 28, 2020 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants