Typescript: Provide a quick fix for uninitalized class properties

Created on 31 Jan 2018  路  14Comments  路  Source: microsoft/TypeScript

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!

Quick Fixes Fixed Suggestion help wanted

All 14 comments

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

I'd like to work on this 馃槂

Go for it!

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?

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.

ping @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?

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;
}

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 = "";
}

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

Sounds reasonable

i have finished codefix work 馃槃
review required

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

@vaskevich I think that would be a bad idea since those would be less compatible with javascript's own class fields (https://github.com/tc39/proposal-class-fields), which IIRC are always initialized to undefined, never missing.

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;

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

wmaurer picture wmaurer  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

dlaberge picture dlaberge  路  3Comments