Skip to content

Design Meeting Notes, 11/17/2017 #20117

Closed
@mhegazy

Description

@mhegazy

Strict property initialization checks in classes

  • abstract properties should be excluded
  • T extends number | underfed is an error, since we do not know if it is one or the other at instantiation time
  • Enabled under new strict mode flag --strictPropertyInitialization
  • Issues:
  1. names in quotes are not checked:
    class B {
       a: number;  // Error
       "b" : number; // No error
    }
  2. Do we need a new explicit modifier?
    • How do you opt-out for a specific class
      • // @ts-ignore
        • seems dangerous to rely on this, you change the type, and now no errors
      • or put it in "b"
        • too cute..
        • this looks like a bug that we should fix in the future
      • new modifier:
        • kotlin uses lateinit, swift has lazy?
        • bang operator: a!: number
        • extend it to locals as well
          • not allowed on const
          • should be an error to have it on something with an initializer
          • readonly is ok to have ! to allow for external
          • does not amke sense for statics
          • @bterlson is not happy about !
            • but we have it in expression positions already
  3. method calls in the constructor
    • this guards against observing un-initialized properties outside the class, but not inside the class
    • this allows for for init patterns
    • it is also easy to get around this using aliases, or even through super access
  • @DanielRosenwasser, all angular projects, mobx, etc.. that use decorators to initialize properties will have to opt-out of these features
  • you can bang these properties
  • it is possible that in the future to allow decorators to change the type of the declaration, and in that world we would not need to bang
  • this is opt-in at the end of the day, and you can keep the world you are used to

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design NotesNotes from our design meetings

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions