-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
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
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:
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
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript