Skip to content

strictNullChecks mode - variable initial value is ignored in the control flow #10640

Closed
@aliai

Description

@aliai

TypeScript Version: nightly (2.0.2)

In strictNullChecks mode only:

Code

    interface IFoo {
      foo?: string;
    };

    var a: IFoo = { foo: '' };
    a.foo.split('');  // <-- Error: a.foo is possibly undefined!

Expected behavior:

This is a valid piece of code. a.foo is defined, therefore, a.foo.split is a valid expression.

Actual behavior:

a.foo is concluded as type string | defined, even though it is defined in the initiation phase var a:IFoo = { foo: '' }. tslint/typescript complains that foo is possibly undefined and therefore, cannot access to split method.

the following code runs without problem simply because foo is assigned after a declaration:

    var a: IFoo = {};
    a.foo: '';
    a.foo.split('');

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedRevisitAn issue worth coming back toSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions