microsoft / microsoft/TypeScript

Allow return type annotations on setters

Open
#61,561 5 comments 0 reactions 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

Setters can return values, so TypeScript should support declaring return types on setters.

### 🔎 Search Terms

setter return type

### 🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for relevant entries

### ⏯ Playground Link

- Without return type: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZiEMC8MDeAoGMIFNYBGAhgE4AUA+gJTpbYwn4CuJYMA5I1C2wG5EAbJrnYBuOgF8ME8aEiw8UKLhIQAwkTAAlZqwBqg4RBQwA8gQBWuYFAB0Ac3ymA7mAAKJEAAcVUAJ4AIrgQwCQAll5QIOQIIAA0HMQk7FS2imQ0yFkcXDww-EIisuAQIAK4tgIg9mQABorKqjDAmgAkaA0q6po63PqGwQD8HOwwAFwwAETsUJMSDLp8AxC1VKJAA
- With return type: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZiEMC8MDeAoGMIFNYBGAhgE4AUA+gJQBcOUJAlmAObpbYwn4CuJYMAOTcofAQDciAGx65BAbg4BfDEsWhIsPFCi4SEAMJEwAJV78AatNkQUMAPIEAVrmBQAdC3z2A7mAAKJCAADnpQAJ4AIrgQwEzBUCDkCCAANELEJIJU7tpkVCjIqMLmEtZy6uAQIFK47lIgLGQABtq6+jDAxgAkaG16hsZmopblEAD8QoIwdABEglCzSlylMJIyMc1U8kA

### 💻 Code

```ts
const foo = {
set bar(_) {
return 'return value';
}
};
const settersCanReturnValues = Object.getOwnPropertyDescriptor(foo, 'bar').set() === 'return value';
console.log(`setters can${settersCanReturnValues? '' : "'t"} return values`);
```

Same code, with an explicit return type:

```ts
const foo = {
set bar(_): string {
return 'return value';
}
};
const settersCanReturnValues = Object.getOwnPropertyDescriptor(foo, 'bar').set() === 'return value';
console.log(`setters can${settersCanReturnValues? '' : "'t"} return values`);
```

### 🙁 Actual behavior

TypeScript compiler emits invalid JavaScript containing return type declaration syntax.

### 🙂 Expected behavior

TypeScript compiler supports and removes return type declarations from setters.

### Additional information about the issue

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

Start with the two TypeScript Playground examples and compare the emitted JavaScript for setters with and without an explicit return type. The change is done when the annotated setter compiles to valid JavaScript with its return type removed, while the existing unannotated example continues to work; add a regression test in the compiler's existing test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.