Closed as not planned
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
π» 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