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

StrictPropertyInitialization does not understand calls to helpers #21132

Closed
berickson1 opened this issue Jan 10, 2018 · 4 comments
Closed

StrictPropertyInitialization does not understand calls to helpers #21132

berickson1 opened this issue Jan 10, 2018 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@berickson1
Copy link

TypeScript Version: 2.7.0-dev.20180110

Code

export class Test {
    private _foo: string;
    constructor() {
        this._init();
    }

    private _init() {
        this._foo = 'test';
    }
}

https://github.com/berickson1/Playground/blob/master/strictPropertyInitError.ts

Expected behavior:
Code compiles with strict flags on

Actual behavior:
strictPropertyInitError.ts(4,13): error TS2564: Property '_foo' has no initializer and is not definitely assigned in the constructor.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jan 10, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jan 10, 2018

The compiler does not analyze all calls from the constructor. for these, use definite assignment operator !. see #20166

@RyanCavanaugh
Copy link
Member

See also long discussion of how this would be an expected effect #8476

@berickson1
Copy link
Author

berickson1 commented Jan 11, 2018

I understand that inheritance could make tracking calls from the constructor tricky if public methods were called.
Has there been any thought regarding just tracking private method calls for variable initialization? We've done something similar to reset internal state of classes for unit testing. In this way we could do the following.

export class Test {
    private _foo: string;
    constructor() {
        this._init(); //<- follow this chain
        this.myMethod();//<- don't follow this chain
    }

    private _init(override?: string) {
        this._foo = override || 'test';
    }
    
    public myMethod() {
        this._foo = 'bar'; // This isn't used to track strict property init
    }

    public resetState() {
        this._init('reset');
    }
}

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants