Skip to content
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

Closed
ThomasKiesel opened this issue Jan 8, 2019 · 5 comments
Closed

Nameclash between properties of class and subclass not found #29305

ThomasKiesel opened this issue Jan 8, 2019 · 5 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@ThomasKiesel
Copy link

ThomasKiesel commented Jan 8, 2019

Code

"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);
}

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:

@j-oliveras
Copy link
Contributor

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)

@ThomasKiesel
Copy link
Author

ThomasKiesel commented Jan 8, 2019 via email

@j-oliveras
Copy link
Contributor

Your correct, compiler should warn that the id getter on B can conflict with id property on A.

@weswigham
Copy link
Member

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.

@jakebailey
Copy link
Member

Checking out old bugs; after #37894, all of the samples in this issue now correctly error as this mixing is now disallowed:

'id' is defined as a property in class 'A', but is overridden here in 'B' as an accessor.(2611)

So, seems like this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants