Skip to content

Shape-based type equivalence should evaluate getters and setters, not just gettersΒ #59382

Closed as not planned

Description

πŸ”Ž Search Terms

type shape equivalency getters setters

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type shape equivalency

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEAi0G8BQ1XTALmgOwK4FsAjAUwCcBuFNQrCAF1IEtsBzStaYAe23tN2B0upABQBKKqmQcOdABaMIAOjDQAvNACMAJgDM7GanmKlhddABEAQQBCAYQsHUAXySukoSDACiiSdAAHJgA3MDpiaAB9ABMsWCdOHj4BIVFYuAkOaUNoY2UY82iE1w4WYjp0cURSctxSbFyFfOiVchK0MorCKoQaujqGvKUY0za3JCQAM1xsQUYeaEnsEXTYMWgsYK5GaKRs1BbVDV0T8lQAenPofmw6RnwIybBGEDqIkmAwXAgI+TJiADkMGwXGgPzo4VI4yQIHK0GIWF8GmwxAA7tBvCIUejYOIxJQpstiPiLlcIHIuLgQNFrsQAsIKr4QRVuPgAmFGIRYdBUYx5HBoFjQeDIRAJEA

πŸ’» Code

class D {
    a: number;
    b: string;
    constructor()
    {
        this.a = 123;
        this.b = "ABC";
    }
}

class E {
    private _d: D;
    constructor(d: D)
    {
        this._d = d;
    }
    get a() {return this._d.a;}
    get b() {return this._d.b;}
}

function fn(d: D) : void
{
    d.a = 333;  // runtime failure because there's no setter
}

let e: E = new E(new D());

fn(e);  // should report E not compatible with D (no setters)

πŸ™ Actual behavior

No errors are reported when passing and instance of E in a call where D is expected because E has getters for all properties of D, even though only getters exist and there are no setters. As a result of this, a runtime error will be reported if an attempt to assign any of those properties is made.

πŸ™‚ Expected behavior

The type checker should consider type shapes equivalent only if they have same get/set behavior. In the example above, if a and b in D were readonly, OR if E had get/set for both properties, then the example would contain no errors. Both of these conditions should be checked when evaluating type shape equivalency.

Additional information about the issue

TypeScript 5.5.3

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

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