microsoft / microsoft/TypeScript

Support declaring multiple setter overloads

Open
#60,664 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔍 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) {
    // ...
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the proposed setter overload syntax and motivating example in the issue. Determine how multiple setter signatures should be type-checked while preserving one implementation and unchanged JavaScript output; done means the feature is supported without changing existing runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.