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

Readonly vars getting typecast to "false" instead of "boolean" #13113

Closed
deregtd opened this issue Dec 22, 2016 · 6 comments
Closed

Readonly vars getting typecast to "false" instead of "boolean" #13113

deregtd opened this issue Dec 22, 2016 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@deregtd
Copy link

deregtd commented Dec 22, 2016

TypeScript Version: 2.1.4

Code

class blah {
    private readonly _something = false;
    constructor() {
        this._something = true;
    }
}

Expected behavior:
Doesn't cause an error

Actual behavior:
Causes an error

@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Dec 22, 2016

class blah {
    private readonly _something: boolean;
    constructor() {
        this._something = true;
    }
}

#10676

The type inferred for a const variable or readonly property without a type annotation is the type of the initializer as-is.

@deregtd
Copy link
Author

deregtd commented Dec 22, 2016

I guess "by design" is an answer. Not an answer I'd expect for this, but fair enough.

@RyanCavanaugh
Copy link
Member

Can I ask why there are two initializers for _something ?

@deregtd
Copy link
Author

deregtd commented Dec 22, 2016

Because _something has a default value, and only some paths in the initializer set it. It was an easy fix to change it to "private readonly _something: boolean = false", it just seems an odd language construct. Readonly is only readonly after the end of the constructor. If anything, it feels like you should take the type as the union of all options set in the validity period, so in this case would be false|true.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 22, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 22, 2016

This has already been discussed in #12189 in the context of #12148. The conclusion then was that this code is ambiguous, and the property should have a type annotation. in the absence of a type annotation, the compiler will use the type from the internalizer, i.e. false in this case.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 22, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 22, 2016

Duplicate of #12148.

@mhegazy mhegazy closed this as completed Apr 21, 2017
@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

5 participants