Skip to content

Should getters still infer the same type from setters over their return expressions? #43414

Closed

Description

class C {
    #x = Promise.resolve("")

    set myValue(x: Promise<string> | string) {
        this.#x = Promise.resolve(x);
    }
    get myValue() {
        return this.#x;
    }
}

Should the get type of myValue be Promise<string> or just string | Promise<string>? Right now it is the latter; but technically, the former provides a more specific type.

Changing this technically would be a breaking change.

Activity

  1. MartinJohns commented on Mar 29, 2021

    @MartinJohns
    Contributor

    Related: #2521 / #42425.

    Personally I would keep it as is, and allow users to opt-in to the new behavior with an explicit type annotation.

  2. ahejlsberg commented on Mar 29, 2021

    @ahejlsberg
    Member

    I agree, I don't think there is a compelling case for us to infer divergent getter and setter types.

  3. RyanCavanaugh commented on Mar 29, 2021

    @RyanCavanaugh
    Member

    I also would prefer to keep the current behavior

  4. added
    DeclinedThe issue was declined as something which matches the TypeScript vision
    and removed on Mar 30, 2021
  5. DanielRosenwasser commented on Mar 30, 2021

    @DanielRosenwasser
    MemberAuthor

    Consensus'd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions