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

Support declaring multiple setter overloads #60664

Open
6 tasks done
peterflynn opened this issue Dec 3, 2024 · 2 comments
Open
6 tasks done

Support declaring multiple setter overloads #60664

peterflynn opened this issue Dec 3, 2024 · 2 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

@peterflynn
Copy link

πŸ” Search Terms

setter overload, multiple setter signatures, multiple setter input types

βœ… Viability Checklist

⭐ Suggestion

TypeScript allows declaring multiple different argument signatures for a function, i.e. "function overloads": https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads.

We should be able to do the same for property setters, since they are modeled much like a function taking a single argument.

(Note that unlike some other languages, TypeScript's overloads are only bare type signatures: there is only a single method body implementation shared by all of them, making this a pure typechecking feature with no impact on the generated runtime code. This proposal works the same way, just extending this syntax from functions/methods to setters as well).

πŸ“ƒ Motivating Example

A setter might want to separate different types of inputs for improved clarity of documentation:

/**
 * Set startTime to a specific timestamp, specified as a Date object or number of ms since epoch.
 */
set startTime(date: Date | number);

/**
 * Set startTime to the start of the most recent activity matching the given category name.
 */
set startTime(category: string);

set startTime(dateOrCategory: Date | number | string) {
    // ...
}

Note that these differ not just in documentation but also in the value argument's name, which often appears in generated docs output too.

πŸ’» Use Cases

Although setter overloads are necessarily less versatile than function overloads with multiple parameters, some of the same rationales for the overload feature still apply to setters – as seen in the example above.

Workaround
As with functions before overloading is supported, the workaround is just to glom all the docs together with some additional verbiage, e.g.:

/**
 * Set startTime:
 * - If given a Date object or number of ms, sets to a specific timestamp.
 * - If given a category name string, sets to the start of the most recent activity matching that name.
 */
set startTime(dateOrCategory: Date | number | string) {
    // ...
}
@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 Dec 3, 2024
@RyanCavanaugh
Copy link
Member

Just my opinion, but this seems like terrible API design if there are so many possible coercions. What are some libraries that do this?

@peterflynn
Copy link
Author

@RyanCavanaugh Sometimes setters are treated essentially as a way to inline construct a new value, so they may wind up offering just as many different overload choices as a constructor method would.

The original DOM API example motivating #43662 is one example of that pattern, albeit not complex enough to need overloads in that case. But the actual property value (as seen in the getter) is a CSSStyleDeclaration object, yet you pass the setter a string which is essentially used to construct one for you. If there was more than one way to construct a CSSStyleDeclaration object, then you might find yourself wanting overloads for a setter like that one.

As to why you might want a setter to act more like a constructor... developer convenience feels like one motivation. Wanting an API where you can't ever create free-floating "orphan" instances of the thing being constructed could be another.

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

2 participants