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 don't works right with delegate initialization #22259

Closed
spat-ne-hochu opened this issue Mar 1, 2018 · 3 comments

Comments

@spat-ne-hochu
Copy link

TypeScript Version: 2.7.2

Code

class A {
    private b: number;
    private c: string;

    constructor(b: number) {
        this.b = b;
        this.initC();
        
    }

    private initC() {
        this.c = 'string';
    }
}

--strictNullChecks
--strictPropertyInitialization

Expected behavior:
no errors

Actual behavior:
Property 'c' has no initializer and is not definitely assigned in the constructor.

Playground Link:
https://goo.gl/uP2yKq

@Kingwl
Copy link
Contributor

Kingwl commented Mar 1, 2018

There are certain scenarios where properties can be initialized indirectly (perhaps by a helper method or dependency injection library), in which case you can use the new definite assignment assertion modifiers for your properties (discussed below).

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html

@spat-ne-hochu
Copy link
Author

@Kingwl Definite
"Assignment Assertions" is a very bad practice, and I will not use it with serious projects, because it can very easily break the code.

it can check the constructor function.
why can not it test other methods for initialization?

@s-ve
Copy link

s-ve commented Mar 1, 2018

Duplicate of #21132 ?

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

3 participants