★ wanayoo — archive 1999 https://github.com/angular/angular/issues/24571Nouvelle recherche | Portail wanayoo
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

Post strictPropertyInitialization flag flip cleanup #24571

Open
rkirov opened this issue Jun 18, 2018 · 6 comments
Open

Post strictPropertyInitialization flag flip cleanup #24571

rkirov opened this issue Jun 18, 2018 · 6 comments

Comments

@rkirov
Copy link
Contributor

@rkirov rkirov commented Jun 18, 2018

I'm submitting a...

[x] Other... Please describe: Tracking issue for internal cleanup.

What needs to be done

In order to quickly turn on strictPropertyInitialization flag in the whole code base, we introduced ! on every non-initialized class field. Each one of those fields needs to be individually checked and decided whether:

  • the field should be initialized. Thus foo!: string, should become foo = 'default'.
  • the field should be marked as optional. Thus foo!: string, should become foo?: string.
  • the field is reliably initialized, but TS cannot infer that (initialization can occur outside the ctor, for example), so ! should be kept.
@alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Jul 3, 2018 •

@rkirov when an input is required, would you mark with a band !? As in my option in you don't know that the consumer will provide that value, thus, a component should be doing check if that value has been actually provided or not. ! should only be used if the value is initialised such as on ngOnInit. that said though, if one used that component using the viewChild etc... the property will be marked that it will never be undefined. Which in some cases it might be wrong, based on which lifecycle hook the value is initialized in.

And seeing the comment from @mhevery in #21571 it seems that we are on the same page;

Nothing forces @input() to be set. A user of the your component may simply chose not to bind to that input in which case your @input() is not initialize and you should have checks for non-initialized case. I know we disagree, but I think this is the correct behavior.

Truth to the told I am not a particular fan of using the !

@rkirov
Copy link
Contributor Author

@rkirov rkirov commented Jul 12, 2018

If you want to have required semantics for your Inputs what we have been recommending is ! and an assert in ngOnInit. If are to be pedantic the assert should be in ngOnChanges, but that might be too much for some.

The general advice for ! in any place (on expressions or in field declarations names) is that it is unsafe and to be avoided unless there is a compelling reason.

I have an additional advice on cleaning up after my change specifically for angular components.

For Angular components

For angular components, use the following rules in deciding between:
a) adding initializer
b) make the field optional
c) leave the '!'

  • If the field is annotated with @input - Make the field optional b) or add an initializer a).
  • If the input is required for the component user - add an assertion in ngOnInit and apply c.
  • If the field is annotated @ViewChild, @ContentChild - Make the field optional b).
  • If the field is annotated with @ViewChildren or @ContentChildren - Add back '!' - c).
  • Fields that have an initializer, but it lives in ngOnInit. - Move the initializer to the constructor.
  • Fields that have an initializer, but it lives in ngOnInit and cannot be moved because it depends on other @input fields - Add back '!' - c).
@alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Jul 12, 2018 •

Thanks for the detailed explanation much appreciated.

I quite like what you suggest above.

@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jul 12, 2018
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 21, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 22, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 24, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 26, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 29, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Jan 31, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 6, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 7, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 8, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 8, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 8, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 8, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
kara added a commit to petebacondarwin/angular that referenced this issue Feb 12, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571
mhevery added a commit that referenced this issue Feb 13, 2019
When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See #24571

PR Close #28055
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Feb 14, 2019
…lar#28055)

When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571

PR Close angular#28055
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 4, 2019
…lar#28055)

When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See angular#24571

PR Close angular#28055
AndrewKushnir added a commit that referenced this issue Mar 4, 2019
…) (#28736)

When we added the strict null checks, the lexer had some `!`
operators added to prevent the compilation from failing.

This commit resolves this problem correctly and removes the
hacks.

Also the comment

```
// Note: this is always lowercase!
```

has been removed as it is no longer true.

See #24571

PR Close #28055

PR Close #28736
@a-deeb
Copy link

@a-deeb a-deeb commented Mar 23, 2019

Hi, I am fairly new to Github but would like to help out, I see a lot of files that have // TODO(issue/24571): remove '!'. in them, I would like to know how would I go about making changes in this file for example : angular/packages/core/test/linker/query_integration_spec.ts and angular/packages/upgrade/src/common/component_info.ts .
Thanks !

@Splaktar
Copy link
Member

@Splaktar Splaktar commented Apr 12, 2019 •

This doesn't look very good / professional in the API docs (especially when repeated many times on one page):
Captura de Pantalla 2019-04-12 a la(s) 02 44 28

Airblader added a commit to Airblader/angular that referenced this issue May 10, 2019
QueryList#first and QueryList#last can both sensibly be undefined if
the QueryList doesn't match anything.

relates angular#24571

Signed-off-by: Ingo Bürk <admin@airblader.de>
Airblader added a commit to Airblader/angular that referenced this issue May 10, 2019
QueryList#first and QueryList#last can both sensibly be undefined if
the QueryList doesn't match anything.

relates angular#24571

Signed-off-by: Ingo Bürk <admin@airblader.de>
@Splaktar Splaktar mentioned this issue Jun 10, 2019
5 of 14 tasks complete
@Splaktar
Copy link
Member

@Splaktar Splaktar commented Jun 10, 2019 •

https://mariusschulz.com/blog/typescript-2-7-strict-property-initialization#solution-4-definite-assignment-assertion has some detailed explanations of this feature and how to use “Definite Assignment Assertion” (i.e. !). It also has guidance for when to use | undefined in the type def in addition to covering the basics of defining the value at declaration time and in the constructor.

I do think that we should prioritize cleaning up these comments, and “doing the proper thing” in each case, as it looks unprofessional in the docs (for 12+ months). But of course, it’s not as high of a priority as Ivy…

It seems like this might be something where the community in general, a GDE, or an Angular Collaborator may be able to help out. There are around 563 occurrences that need to be individually inspected and refactored

@Splaktar Splaktar mentioned this issue Jun 12, 2019
5 of 14 tasks complete
@kapunahelewong kapunahelewong added this to Committed - Selected for development in docs Jul 10, 2019
ayazhafiz added a commit to ayazhafiz/angular that referenced this issue Sep 13, 2019
`nodeModulesPath` is now initialized in the constructor, so there is no
need for a non-null assertion as suggested in angular#24571.
kara added a commit that referenced this issue Sep 13, 2019
…ber (#32666)

`nodeModulesPath` is now initialized in the constructor, so there is no
need for a non-null assertion as suggested in #24571.

PR Close #32666
arnehoek added a commit to arnehoek/angular that referenced this issue Sep 26, 2019
…ber (angular#32666)

`nodeModulesPath` is now initialized in the constructor, so there is no
need for a non-null assertion as suggested in angular#24571.

PR Close angular#32666
atscott added a commit that referenced this issue Jul 14, 2020
…6805)

When we added the strict null checks, the lexer had some `!` operators added to prevent the compilation from failing.

See #24571

PR Close #36805
atscott added a commit that referenced this issue Jul 14, 2020
…6805)

When we added the strict null checks, the lexer had some `!` operators added to prevent the compilation from failing.

See #24571

PR Close #36805
profanis added a commit to profanis/angular that referenced this issue Sep 5, 2020
…gular#36805)

When we added the strict null checks, the lexer had some `!` operators added to prevent the compilation from failing.

See angular#24571

PR Close angular#36805
@jelbourn jelbourn added P4 and removed severity2: inconvenient labels Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
docs
Committed - Selected for development
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants