Skip to content
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

Getting a setter-only property is typed as the setter's parameter's type instead of undefined #60954

Open
Gouvernathor opened this issue Jan 12, 2025 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Gouvernathor
Copy link

πŸ”Ž Search Terms

setter only

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, from 5.7.3 down to 3.3.3333

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/MYewdgzgLgBANgQzAcwK4OQUxgXhgbwCgYYJNZhUAnKzMKACjAQFtMAuUqKgSxQEoCxEjCgALHhAB0cEMikAHVBDFNWmfgG5hAXwA0w2ck4BtALowEELrxTmDO7YUQp0WKZRp1YeAEQAxACVfJ1BIClx4JDQMTA9qWnptIA

πŸ’» Code

const language = {
  set current(name: string) {
    this.log.push(name);
  },
  log: [] as string[],
};

language.current = "FR";

const c = language.current;

πŸ™ Actual behavior

Variable c is said to be of type string.

πŸ™‚ Expected behavior

As clearly stated here, it should be undefined.

Note that current is not defined, and any attempts to access it will result in undefined.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jan 12, 2025

Effectively #30852; TS doesn't have a "setter only" or "writeonly" concept. But now that TS has #53417, it seems like a missing getter could be treated like get x(): undefined to address this?

@Gouvernathor
Copy link
Author

Gouvernathor commented Jan 12, 2025

That sounds good. Except that when said variable is a #private, it throws an exception instead of returning undefined... but that's probably not the same issue.

Maybe it could be typed as never instead of undefined ? To more emphasize the issue, maybe... Or void, which would be even more accurate ?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jan 13, 2025
@RyanCavanaugh
Copy link
Member

Implicitly making a get/set that isn't reflexive (i.e. illegal to write a.x = a.x;) is kind of suspicious, and this might be breaky for some codebases. It's a possible change but would probably need stronger motivation; simply forgetting to write a getter is pretty difficult.

@Gouvernathor
Copy link
Author

Gouvernathor commented Jan 13, 2025

You may believe it to be suspiscious, but it's a practice that's fully and explicitly supported by MDN (I don't have the courage to read ECMA specs but save from that, I guess it's the best source of authority in the matter). It's more common to have a getter-only than a setter-only property, sure, but still it is also useful on its own.

I use it as a seed-setter for RNG objects, and using a setter property rather than a setter method allows several generators to be given the same seed at the same time without having to define a variable. Sure, it's not the most crucial feature I could think off, but if it's explicitly part of the standard, it's not ours to second-guess.

And you could also concievably rely on that to make contravariant property types... though I did not encounter it, it's theoretically a valid use-case.

And finally, could you give an example of code where this would be a breaking change ?

@Gouvernathor
Copy link
Author

Ok, fair point. But as per the examples above, I still think it's a useful feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants