8000 Compile time comparison of accessors fails to differentiate on setter/getter · Issue #49046 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Compile time comparison of accessors fails to differentiate on setter/getter #49046
@ghost

Description

Bug Report

🔎 Search Terms

  • type check read-only accessor
  • compile time check for setter

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type checking of accessors

⏯ Playground Link

Playground link with relevant code

💻 Code

class Immutable {
    get prop(): string {
        return 'foo';
    }
}

class Mutable {
    set prop(value: string) {}
}

class Other {
    get prop2(): string {
        return 'foo';
    }
}

const test = (v: Mutable) => {};

test(new Other());
//Compile error as expected
test(new Immutable());
//No error, but would also expect one

🙁 Actual behavior

Other correctly causes a compile error as it does not have prop property. Immutable passes compiler check even though the prop property is read-only (just a get)

🙂 Expected behavior

I would expect both Other and Immutable to fail the typing of test as neither adhere to the structure of Mutable.

If inside test I try and set v.prop when Immutable is passed in I get a runtime error. I would expect the typing to guard against this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0