microsoft / microsoft/TypeScript
Allow return type annotations on setters
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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