You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide a quick fix for uninitalized class properties #21509
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!
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)
Activity
mhegazy commentedon Jan 31, 2018
I am assuming we will provide 3 actions:
| undefinedto the type declarationKingwl commentedon Feb 1, 2018
I'd like to work on this 😃
mhegazy commentedon Feb 1, 2018
Go for it!
Kingwl commentedon Feb 1, 2018
How should we set their initializer expression?
mhegazy commentedon Feb 1, 2018
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.
Kingwl commentedon Feb 2, 2018
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
what should
class Tprops do after codefix?or
or other way?
mhegazy commentedon Feb 2, 2018
you do not need the
= undefined.I think we should have 3 code actions:
| undefinedor 3. add a definite assignment operator
Kingwl commentedon Feb 2, 2018
thanks for your response
and if the TT has the constructor
then should i create a instance of TT in the prop b?
mhegazy commentedon Feb 2, 2018
Sounds reasonable
Kingwl commentedon Feb 2, 2018
i have finished codefix work 😄
review required
vaskevich commentedon Mar 8, 2018
Nice! Instead of "Add 'undefined' type to property '{0}'", did you consider making the property optional via, e.g.,
prop?: string;?SlurpTheo commentedon Mar 16, 2018
We recently (probably with 2.7's
--strictPropertyInitialization) discovered a problem where we thought we had declared an optional property:But that's actually:
And that's harder to "back-into" had we just used this syntax from the get-go:
DanielRosenwasser commentedon Mar 26, 2018
Oleg Vaskevich (@vaskevich) SlurpTheo I'd recommend filing a new issue; if a lot of users end up requesting that, we can consider it.