Skip to content

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

Open
eligrey opened this issue Apr 10, 2025 · 5 comments
Open

Allow return type annotations on setters #61561

eligrey opened this issue Apr 10, 2025 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@eligrey
Copy link

eligrey commented Apr 10, 2025

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

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

@eligrey eligrey changed the title Bug: Unable to bypass TS2408 error: Setters cannot return a value Bug: 'TS2408: Setters cannot return a value' error is incorrect Apr 10, 2025
@RyanCavanaugh
Copy link
Member

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 @ts-ignore.

@eligrey
Copy link
Author

eligrey commented Apr 10, 2025

@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?

@eligrey
Copy link
Author

eligrey commented Apr 10, 2025

@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`);

@eligrey eligrey changed the title Bug: 'TS2408: Setters cannot return a value' error is incorrect Bug: Setters cannot have return types Apr 10, 2025
@RyanCavanaugh
Copy link
Member

Should I close this issue, or do you think it's worth re-scoping to improve the DX here?

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.

@eligrey
Copy link
Author

eligrey commented Apr 10, 2025

I re-scoped this to the return types compilation bug; feel free to further re-scope as a feature request

@RyanCavanaugh RyanCavanaugh changed the title Bug: Setters cannot have return types Allow return type annotations on setters Apr 10, 2025
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants