Typescript: strictPropertyInitialization don't works right with delegate initialization

Created on 1 Mar 2018  路  3Comments  路  Source: microsoft/TypeScript

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

Most helpful comment

@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?

All 3 comments

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

@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?

Duplicate of #21132 ?

Was this page helpful?
0 / 5 - 0 ratings