Skip to content

Allow return type annotations on setters #61561

Open
@eligrey

Description

@eligrey

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions