GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Design Meeting Notes, 11/17/2017 · Issue #20117 · microsoft/TypeScript

Design Meeting Notes, 11/17/2017 #20117

New issue
Copy link
New issue
Copy link
Closed
Closed
Design Meeting Notes, 11/17/2017#20117
Copy link
Labels
Design NotesNotes from our design meetingsNotes from our design meetings

Description

@mhegazy
Mohamed Hegazy (mhegazy)
opened on Nov 17, 2017
Issue body actions

Strict property initialization checks in classes

  • PR: Strict property initialization checks in classes #20075
  • Every non-initialized property that does not have undefined in it type will be an error if not definitely assigned in the constructor, (meaning all code paths needs to have an initializer).
  • 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
    }
    • we do not do control flow analysis on x["foo"] currently, we only do it on x.foo.
    • we could do that if we address Discriminant property type guard not applied with bracket notation #10530
    • Nathan Shively-Sanders (@sandersn) did have a PR to address that, and the problem was perf
    • also applies to symbols, or any computed property names
  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
          • Brian Terlson (@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
  • Daniel Rosenwasser (@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
Reacted by Herrington Darkholme

Activity

  1. Mohamed Hegazy (mhegazy)
    closed this as completedon Nov 17, 2017
  2. Mohamed Hegazy (mhegazy)
    added
    Design NotesNotes from our design meetingsNotes from our design meetings
    on Nov 17, 2017
  3. DanielRosenwasser commented on Nov 17, 2017

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Nov 17, 2017
    Member
    More actions

    My notes:


    Strict Property Initialization Checks in Classes

    • Does adding | undefined to a declaration, do we still do initialization checking within the constructor?
      • No, it's in the domain, allowed not to initialize it.
      • What about with generics that have undefined as a constraint?
        • No, only signals it could be instantiated with undefined
    • What about @decorated property declarations?
      • People can always opt out of this.
      • Code in the RWC doesn't break because they don't opt into strict.
    • Does it make sense to have a modifier for this?
      • ! after the property name.
      • lateinit? Some sort of modifier to assure the compiler it doesn't need to write any checks.
      • Should we error on initialized declarations like a!: number = 10?
        • Probably should let linters take over this.
      • Make it !, it reflects expression level non-null assertion logic.
        • Allow it on locals.
        • But don't allow it on const.
        • Also, probably error when we give an initializer (nix that last point about linters taking this on)
        • But allow it on readonly.
    • What about local methods that can be called from the constructor that might observe uninitialized values.
      • Only so far we can go.
  4. tinganho commented on Nov 19, 2017

    @tinganho
    Tingan Ho (tinganho)
    on Nov 19, 2017
    Contributor
    More actions

    you can bang these properties

    This sounds funny.

    Nearly all of our classes are handled by some sort of IoC container.

    Sometimes(99% in my case) classes, are just definitions and construction is "inversioned". The IoC container takes care of construction of the class. I don't want the compiler to complain in these type of classes. And providing a flag to opt-out or banging all the properties doesn't sound optimal either.

    One suggestion, I can think of, at least for the decorator use case, is to have a modifier on the "definition" class decorator.

    In the below example you can see a constructtarget modifier:

    export function Entity(definitions?: EntityDefinitions) {
        return constructtarget function(target: any) {
            //...
        };
    }

    And applying it on entity definitions will not cause any errors:

    @Entity()
    class User {
        @Column()
        name: string; // no error
        @Column()
        age: number; // no error
    }

    Example from TypeOrm https://github.com/typeorm/typeorm.

    Reacted by Herrington Darkholme
  5. DanielRosenwasser commented on Nov 19, 2017

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Nov 19, 2017
    Member
    More actions

    We discussed decorators that can declare how they modify the target, so it's an option, but others in the meeting felt like doing so shouldn't be a blocker for the flag as it exists today.

  6. mihailik commented on Nov 22, 2017

    @mihailik
    mihailik
    on Nov 22, 2017
    Contributor
    More actions

    Should this flag hold off from graduating to --strict set?

    If it needs to be disabled for a all/large subset of angular code out there, perception of TS as a stable platform might be affected.

  7. mhegazy commented on Nov 22, 2017

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Nov 22, 2017
    ContributorAuthor
    More actions

    Should this flag hold off from graduating to --strict set?

    we have the infrastructure in place to opt-out of specific --strict flags as needed. --strict --strictPropertyInitialization false. --strict by definition means you are accepting breaking changes between releases. if this is not your intention, then do not use --strict and instead spell out the flags you are interested in, e.g. --noImplictAny --strictNullchecks

    Reacted by mihailik
  8. mihailik commented on Nov 22, 2017

    @mihailik
    mihailik
    on Nov 22, 2017
    Contributor
    More actions

    Hope you're right — regardless of technical reasoning you still want gain > pain to proceed

  9. tinganho commented on Nov 22, 2017

    @tinganho
    Tingan Ho (tinganho)
    on Nov 22, 2017
    Contributor
    More actions

    I think many people would rather bang all properties than setting the flag, to just keep the default "strict" mode. But I hope the TS team can hold this off a bit, certainly a contentious topic. As of right now, it feels like a choice between pest and colera.

  10. microsoft
    locked and limited conversation to collaborators on Jun 14, 2018
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

    Design NotesNotes from our design meetingsNotes from our design meetings

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.