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

set only property bug #4839

Closed
RichardEll opened this issue Sep 17, 2015 · 6 comments
Closed

set only property bug #4839

RichardEll opened this issue Sep 17, 2015 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@RichardEll
Copy link

If you define a class that has a property which only has a setter, but not a getter, and then attempt to use the non-existent getter the code will compile, but the getter is undefined, leading to a crash when it is used. For example:

class MyClass {

    private theCount: number;

    constructor() {
        this.theCount = 5;
    }

    public set count(i: number) {
        this.theCount = i;
    }
}

window.onload = () => {
    var obj: MyClass = new MyClass();
    obj.count = 12;
    var num: number = obj.count;
    document.getElementById("content").innerHTML = num.toString();
};

The above will compile, but crashes when reading obj.count.
Surely it would be better if this code did not compile.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 18, 2015

se #12 for more information.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 18, 2015
@mhegazy mhegazy closed this as completed Sep 18, 2015
@RichardEll
Copy link
Author

Is this really a duplicate? #12 is a suggestion for a read only modifier. I am not advocating that. I just think that if no getter is defined then the compiler should flag an error at compile time if you attempt to use it (like C# does).

@kitsonk
Copy link
Contributor

kitsonk commented Sep 18, 2015

Write only property descriptors are perfectly valid JavaScript, therefore should be perfectly valid TypeScript. While it may not be a very useful programming paradigm, it is a legitimate one.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 18, 2015

Actually, I see what you want, you want #295 which is to throw when you attempt to access a write-only property (or a read-only property). As @RyanCavanaugh states, the type system doesn't currently support that differentiation.

@RichardEll
Copy link
Author

I see the problem.
I discovered this little quirk when I made a class that allowed a particular property (something like a shape dimension - I forget) to be changed. At the time I didn't see the need for the ability to read the value back and, being lazy, didn't bother to implement it. But, some time later, I wrote some code which did attempt to read the value back and was rather puzzled when the code crashed out when it read undefined. I'm pretty sure this won't be the last time I do this sort of thing and it would be great if the compiler would stop me doing this. But I appreciate there are technical reasons why this is difficult.

@ghost
Copy link

ghost commented Sep 23, 2015

Yes, this issue is currently a plague to typescript. It should be addressed by typescript team.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants