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

Class members that are non-optional or are not unioned with undefined don't require initialisation. #20829

Closed
kujon opened this issue Dec 20, 2017 · 1 comment

Comments

@kujon
Copy link
Contributor

kujon commented Dec 20, 2017

TypeScript Version: 2.6.2 strict mode

Apologies if this is a duplicate, I wasn't able to find a relevant issue.

Code

class Foo {
    test: number;
}

const foo = new Foo();

foo.test; //> undefined

Expected behavior:
I'd expect the code above to blow up, because I've not assigned a value to the test member. The compiler could require me to do either of the following to address the issue (similarly to how e.g. Java behaves):

  1. Initialise in place:
class Foo {
    test: number = 42;
}
  1. Initialise in the constructor:
class Foo {
    test: number;
    constructor() {
        this.test = 42;
    }
}

Actual behavior:
The above code compiles.

@j-oliveras
Copy link
Contributor

That will be one of the features of the next version. The pull request #20075 implement this. Roadmap with 2.7 features.

You can try this feature with npm install -g typescript@next (more info about trying future versions).

@kujon kujon closed this as completed Dec 20, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants