Skip to content

Post strictPropertyInitialization flag flip cleanup #24571

Closed

Description

@rkirov

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.

Activity

  1. added this to the needsTriage milestone on Jun 21, 2018
  2. alan-agius4 commented on Jul 3, 2018

    @alan-agius4
    Contributor

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

  3. rkirov commented on Jul 12, 2018

    @rkirov
    ContributorAuthor

    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).
  4. alan-agius4 commented on Jul 12, 2018

    @alan-agius4
    Contributor

    Thanks for the detailed explanation much appreciated.

    I quite like what you suggest above.

  5. modified the milestones: needsTriage, Backlog on Jul 12, 2018
  6. 153 remaining items

  7. added a commit that references this issue on Dec 22, 2024
  8. angular-automatic-lock-bot commented on Oct 26, 2025

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: coreIssues related to the framework runtimefreq2: mediumtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions