"use strict";
class A
{
constructor(public id: string)
{}
}
class B extends A
{
public get id() { return this._innerId;}
private _innerId: string;
constructor(id: string)
{
super(id);
this._innerId = id;
}
}
try
{
new B("id2")
alert("Erfolg")
}
catch(e)
{
alert(e);
}
superclass A has a public property "id" which is set in the constructor.
subclass B is derived from A, but has a getter on an own property "id".
I expected that the compiler detects the name conflict between the public properties "id" in A and B respectively.
Code
Expected behavior:
superclass A has a public property "id" which is set in the constructor.
subclass B is derived from A, but has a getter on an own property "id".
I expected that the compiler detects the name conflict between the public properties "id" in A and B respectively.
Actual behavior:
The typescript compiler generates javascript. When the code is executed, the javascript runtime throws an exception stating that the property "id" of the subclass cannot be set.
TypeError: setting getter-only property "id"Playground Link:
https://www.typescriptlang.org/play/index.html#src=%22use%20strict%22%3B%0D%0Aclass%20A%0D%0A%7B%0D%0A%20%20%20%20constructor(public%20id%3A%20string)%0D%0A%20%20%20%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aclass%20B%20extends%20A%0D%0A%7B%0D%0A%20%20%20%20public%20get%20id()%20%7B%20return%20this._innerId%3B%7D%0D%0A%0D%0A%20%20%20%20private%20_innerId%3A%20string%3B%0D%0A%20%20%20%20constructor(id%3A%20string)%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20super(id)%3B%0D%0A%20%20%20%20%20%20%20%20this._innerId%20%3D%20id%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Atry%0D%0A%7B%0D%0A%20%20%20%20new%20B(%22id2%22)%0D%0A%20%20%20%20alert(%22Erfolg%22)%0D%0A%7D%0D%0Acatch(e)%0D%0A%7B%0D%0A%20%20%20%20alert(e)%3B%0D%0A%7D%0D%0A
Related Issues: