-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Conversation
More and more devs are using TS strict mode as it's advocated by TS team. So generate Angular with strict mode activated.
@@ -37,7 +37,7 @@ export default class ButtonComponent { | |||
* @required | |||
*/ | |||
@Input() | |||
label: string; | |||
label = 'Button'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, welcome back!! 🎉
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for that. TypeScript 🤦
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️ Welcome back ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
Issue: #12070
What I did
How to test