Adds ability to optionalize class getters#16344
Adds ability to optionalize class getters#16344bradenhs wants to merge 3 commits intomicrosoft:masterfrom
Conversation
|
Hey @bradenhs thanks for the PR! One thing that I'd like to ask is whether you could break your tests into multiple files. It seems like there's a lot of stuff you're trying to test (including intentional syntax errors), so I think it'd be a good idea to break that into multiple granular tests. |
|
No problem. I'm traveling now but should have some time within the next week or so to make that change. I would love any other feedback in the meantime. |
|
Would love to see this functionality as well, any updates? |
|
Started first post-college job this summer. Don't have as much coding free time as I once had :) But I have plans to get back to this again before the year is out. |
|
Any updates? |
|
@Andy-MS and @weswigham can you please review |
|
So I've been putting off crossing the finish line with this for a while but would definitely love any additional reviews! It might give me the motivation to finish this up. |
|
Why only |
|
So bear with me as I try to remember my thinking on this. I believe there are two reasonable choices for how to implement this:
I went with the second of those choices because marking |
|
If I understand the PR correctly, its effect is that when a class is used as an interface, the implementer doesn't have to provide a getter. So, what if you have: class C {
get x?() { return 0; }
set x(value: number) { ... }
}
const impl: C = { }; // Should error?Shouldn't I need both the getter and setter to be optional for compilation to succeed? After all, the following is a compile error today: class C {
set x(value: number) { }
}
const impl: C = {}; // Error, needs a setter for 'x' |
|
FYI: I probably won't get to this within the next couple weeks. I still want to finish it up but it'll be slow going. |
|
To anyone who wants to take this PR over feel free to do so. I'd still like to finish it up but evidently it's not a priority because I'm not making time for it :) I may get to it eventually but don't want to keep someone else from tackling this if they're interested! |
|
@bradenhs thanks for the work so far! Will let someone else pick this up if they're able to. |
|
Thanks for the work @bradenhs . It sucks that no one has the time to finish this off (like myself). This feature would have come in handy for me. |
|
@RyanCavanaugh could you put down what needs to be added to get this work completed? Willing to take it up |
|
How can this possibly have been abandoned? Seems absolutely fundamental, essential. What is the work-around? |
|
Here is what I did to get around this: constructor() { val?: string |
|
Ping - too bad this wasn't merged, super want this ability |
|
when will this get solved? |
|
Don't know why this was not prioritized. 😞 |
|
2024 and this is still not fixed |
|
The TypeScript team hasn't accepted the linked issue #14417. If you can get it accepted, this PR will have a better chance of being reviewed. |
Fixes #14417
This PR makes it to possible to optionalize class getters in the same way class methods can be marked optional:
The above code should now work whereas before there was a syntax error. This is my first TypeScript PR so I hope I covered everything. This PR should handle:
strictNullChecksworks as expected with optional class gettersHopefully I didn't miss anything, but I am of course more than happy to fix any issues.