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

Property without a setter, typescript isn't catching error when settting #1810

Closed
RoyM33 opened this issue Jan 26, 2015 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@RoyM33
Copy link

RoyM33 commented Jan 26, 2015

if you create a "property" with a public getter and no setter. if you then try to set that property typescript doesn't tell you this isn't allowed. But it sure will blow up your code.

    private _date: Date;
    public get Date()
    {
        return this._date;
    }

In the above scenario if you do
this.Date = new Date();
typescript allows this without catching anything (no red underbar or error), but the javascript doesn't allow it. Changing the code to the below code works as expected.
this._date = new Date();

@danquirk
Copy link
Member

See #12

@danquirk danquirk added the Duplicate An existing issue was already created label Jan 26, 2015
@buzinas
Copy link

buzinas commented Aug 15, 2015

In fact #12 and this one is completely different.

The other is talking about readonly properties in interfaces (something that the current language does not support). This one is talking about a simple validation to throw a compiler error when trying to change a getter only property.

This one is very simple to implement, totally different from the other.

Both are very important, but for me, this one is a must.

@RyanCavanaugh
Copy link
Member

"Property without a setter" and "Property that is read-only" is a distinction without a difference.

For example, if we have some class

class Foo {
  get bar() { return 0 }
}

This is its corresponding .d.ts:

class Foo {
  bar: number;
}

Without a readonly modifier, there's no way to represent this except when we can see the implementation. Trying to implement this as via "see if there's a setter" would be a hack.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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

4 participants