-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a quick fix for uninitalized class properties #21509
Comments
I am assuming we will provide 3 actions:
|
I'd like to work on this 😃 |
Go for it! |
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
what should
or
or other way? |
you do not need the I think we should have 3 code actions:
class T {
c: number | undefined ;
b: TT | undefined ;
a: string | undefined;
}
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
then should i create a instance of TT in the prop b?
|
Sounds reasonable |
i have finished codefix work 😄 |
Nice! Instead of "Add 'undefined' type to property '{0}'", did you consider making the property optional via, e.g., |
@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 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. |
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!The text was updated successfully, but these errors were encountered: