Skip to content

Compile time comparison of accessors fails to differentiate on setter/getterΒ #49046

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