Skip to content

Provide a quick fix for uninitalized class properties #21509

Closed

Description

class Point {
    z: string;
    constructor() {
    }
}

We should provide a quick fix here to provide a definite assignment assertion on z under --strictPropertyInitialization. Bonus points if we only provide the quick fix when the property has actually been written to!

Activity

  1. mhegazy commented on Jan 31, 2018

    @mhegazy
    Contributor

    I am assuming we will provide 3 actions:

    • Add definite assignment operator (assuming it was written to at least once in the class)
    • Add an initialization in the constructor (assuming we can initialize it, e.g. string, number, or a type that has a constructor)
    • Add | undefined to the type declaration
  2. Kingwl commented on Feb 1, 2018

    @Kingwl
    Contributor

    I'd like to work on this 😃

  3. mhegazy commented on Feb 1, 2018

    @mhegazy
    Contributor

    Go for it!

  4. Kingwl commented on Feb 1, 2018

    @Kingwl
    Contributor

    Add definite assignment operator (assuming it was written to at least once in the class)
    Add an initialization in the constructor (assuming we can initialize it, e.g. string, number, or a type that has a constructor)

    How should we set their initializer expression?

  5. mhegazy commented on Feb 1, 2018

    @mhegazy
    Contributor

    something like https://github.com/Microsoft/TypeScript/blob/master/src/services/codefixes/fixAddMissingMember.ts#L105

    first there has to be a constructor. I would ignore the case where there is no constructor at the moment.

  6. Kingwl commented on Feb 2, 2018

    @Kingwl
    Contributor

    ping Mohamed Hegazy (@mhegazy)
    I'm not sure I understand your meaning correctly

    for example:
    https://www.typescriptlang.org/play/index.html#src=class%20TT%20%7B%0D%0A%7D%0D%0A%0D%0Aclass%20T%20%7B%0D%0A%20%20%20%20c%3A%20number%3B%0D%0A%20%20%20%20b%3A%20TT%3B%0D%0A%20%20%20%20a%3A%20string%3B%0D%0A%7D%0D%0A

    class TT {
    }
    
    class T {
        c: number;
        b: TT;
        a: string;
    }
    
    

    what should class T props do after codefix?

    class T {
        c: number | undefined = undefined;
        b: TT | undefined = undefined;
        a: string | undefined = undefined;
    }
    
    

    or

    class T {
        c: number = 0;
        b: TT = new TT();
        a: string = "";
    }
    
    

    or other way?

  7. mhegazy commented on Feb 2, 2018

    @mhegazy
    Contributor

    you do not need the = undefined.

    I think we should have 3 code actions:

    1. add | undefined
    class T {
        c: number | undefined ;
        b: TT | undefined ;
        a: string | undefined;
    }
    1. add an intializer
    class T {
        c: number = 0 ;
        b: TT;
        a: string = "";
    }

    or 3. add a definite assignment operator

    class T {
        c!: number;
        b!: TT;
        a!: string;
    }
  8. Kingwl commented on Feb 2, 2018

    @Kingwl
    Contributor

    thanks for your response

    and if the TT has the constructor

    class TT {
        constructor () {}
    }
    

    then should i create a instance of TT in the prop b?

    
    class T {
        c: number = 0 ;
        b: TT = new TT(); // this
        a: string = "";
    }
    
    
  9. mhegazy commented on Feb 2, 2018

    @mhegazy
    Contributor

    then should i create a instance of TT in the prop b?

    Sounds reasonable

  10. Kingwl commented on Feb 2, 2018

    @Kingwl
    Contributor

    i have finished codefix work 😄
    review required

  11. ghost closed this as completedin #21528on Feb 23, 2018
  12. vaskevich commented on Mar 8, 2018

    @vaskevich

    Nice! Instead of "Add 'undefined' type to property '{0}'", did you consider making the property optional via, e.g., prop?: string;?

  13. SlurpTheo commented on Mar 16, 2018

    @SlurpTheo

    We recently (probably with 2.7's --strictPropertyInitialization) discovered a problem where we thought we had declared an optional property:

      c: () => number | undefined;

    But that's actually:

      c: () => (number | undefined);

    And that's harder to "back-into" had we just used this syntax from the get-go:

      c?: () => number;
  14. DanielRosenwasser commented on Mar 26, 2018

    @DanielRosenwasser
    MemberAuthor

    Oleg Vaskevich (@vaskevich) SlurpTheo I'd recommend filing a new issue; if a lot of users end up requesting that, we can consider it.

  15. locked and limited conversation to collaborators on Jul 25, 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

    Domain: LS: Quick FixesEditor-provided fixes, often called code actions.FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions