-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow return type annotations on setters #61561
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
Comments
All "errors" are called "errors" because we don't have anything called a "warning", since the definition of what's an error and what's a warning is subject to endless debate. You're always free to ignore an "error" with |
@RyanCavanaugh Ah, thank you for the clarification! I was able to bypass the error as such: const foo = {
set bar(_) {
/* eslint-disable no-setter-return, @typescript-eslint/ban-ts-comment */
// @ts-ignore
return 'return value';
/* eslint-enable no-setter-return, @typescript-eslint/ban-ts-comment */
}
};
const settersCanReturnValues = Object.getOwnPropertyDescriptor(foo, 'bar').set() === 'return value';
console.log(`setters can${settersCanReturnValues? '' : "'t"} return values`); Should I close this issue, or do you think it's worth re-scoping to improve the DX here? |
@RyanCavanaugh notably, set accessors cannot have return types, which is a related bug that causes actual compilation issues. e.g. const foo = {
set bar(_): string {
/* eslint-disable no-setter-return, @typescript-eslint/ban-ts-comment */
// @ts-ignore
return 'return value';
/* eslint-enable no-setter-return, @typescript-eslint/ban-ts-comment */
}
};
const settersCanReturnValues = Object.getOwnPropertyDescriptor(foo, 'bar').set() === 'return value';
console.log(`setters can${settersCanReturnValues? '' : "'t"} return values`); |
I would take a very tactical change to the error message if someone comes up with a message that's equivalently good at conveying the point in the common case (which I think is just a person thinking they're in the getter when they're in the setter, or thinking they're inside a closure but not), but it's probably just best left alone. |
I re-scoped this to the return types compilation bug; feel free to further re-scope as a feature request |
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
💻 Code
Same code, with an explicit return type:
🙁 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
The text was updated successfully, but these errors were encountered: