Skip to content

Allow getter and setter types to be unrelated - #53417

Merged
Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:feature/43662
Mar 30, 2023
Merged

Allow getter and setter types to be unrelated#53417
Andrew Branch (andrewbranch) merged 2 commits into
microsoft:mainfrom
andrewbranch:feature/43662

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Mar 21, 2023

Copy link
Copy Markdown
Member

Fixes #43662

The one thing to note/discuss here is that to create unrelated accessors, both the getter and setter must have explicit annotations, because the setter’s type annotation will be used for an unannotated getter return type:

// Error:
const unannotatedGetter = {
  get p() {
    return 0;
//  ^^^^^^^^ Type 'number' is not assignable to type 'string'
  },
  set p(value: string) {
    // ...
  },
};

// Ok:
const bothAnnotated = {
  get p(): number {
    return 0;
  },
  set p(value: string) {
    // ...
  }
};

I think this is probably better than making it too easy to accidentally create unrelated accessor types, but perhaps it’s not obvious from the error message that unrelated accessor types are possible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM implementation-wise; let's double-check that we want to do this at the next meeting

@sandersn

Copy link
Copy Markdown
Member

:shipit:

@shellscape

Copy link
Copy Markdown

Andrew Branch (@andrewbranch) which version did this land in? having trouble finding it in changelogs

@jakebailey

Copy link
Copy Markdown
Member

5.1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Allow setter type to be incompatible with the getter type

7 participants