-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nameclash between properties of class and subclass not found #29305
Comments
I think this works as expected. Using a javascript equivalent (I changed "use strict";
class A
{
constructor(id)
{
this.id = id;
}
}
class B extends A
{
get id() { return this._innerId;}
constructor(id)
{
super(id);
this._innerId = id;
}
}
try
{
new B("id2")
console.log("Erfolg")
}
catch(e)
{
console.log(e);
} I get:
|
I still have this problem, I can also reproduce it with the latest compiler version (Version 3.3.0-dev.20190108)
Please note: Your code sample significantly differs, the yellow parts are essential for reproduction:
class A
{
constructor(public id)
{
this.id = id;
}
}
class B extends A
{
get id() { return this._innerId;}
private _innerId: string;
constructor(id)
{
super(id);
this._innerId = id;
}
}
The compiler still has no errors. When I run the program, I get (with node v10.14.1)
TypeError: Cannot set property id of #<B> which has only a getter
at new A (C:\tfs\Extensions\DiagnosisExtensions\TypescriptBug\TypescriptBug\app.js:4:17)
at new B (C:\tfs\Extensions\DiagnosisExtensions\TypescriptBug\TypescriptBug\app.js:11:9)
at Object.<anonymous> (C:\tfs\Extensions\DiagnosisExtensions\TypescriptBug\TypescriptBug\app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
I hope this helps.
Von: Jordi Oliveras Rovira <notifications@github.com>
Gesendet: Dienstag, 8. Januar 2019 14:34
An: Microsoft/TypeScript <TypeScript@noreply.github.com>
Cc: Kiesel, Thomas, Dr. <Thomas.Kiesel@comma-soft.com>; Author <author@noreply.github.com>
Betreff: Re: [Microsoft/TypeScript] Nameclash between properties of class and subclass not found (#29305)
I think this works as expected.
Using a javascript equivalent (I changed alert to console.log) and running on node 8.14.1):
"use strict";
class A
{
constructor(id)
{
this.id = id;
}
}
class B extends A
{
get id() { return this._innerId;}
constructor(id)
{
super(id);
this._innerId = id;
}
}
try
{
new B("id2")
console.log("Erfolg")
}
catch(e)
{
console.log(e);
}
I get:
TypeError: Cannot set property id of #<B> which has only a getter
at new A (Z:\tmp\ts\index.js:6:17)
at new B (Z:\tmp\ts\index.js:16:9)
at Object.<anonymous> (Z:\tmp\ts\index.js:23:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#29305 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/Ap_XAU8wqW_u1HPnthRKoR74kJcH-vQ7ks5vBJ43gaJpZM4Z1JPJ>.
|
Your correct, compiler should warn that the |
The root of the issue is that we shouldn't allow overwriting a read/write property with a writeonly one, but we don't track writeonliness, which leads to the issue. |
Checking out old bugs; after #37894, all of the samples in this issue now correctly error as this mixing is now disallowed:
So, seems like this can be closed. |
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:
The text was updated successfully, but these errors were encountered: