Skip to content

Strict null assertion error on an already-defined object property #23791

Closed
@an5rag

Description

@an5rag

TypeScript Version: 2.8.1

Code

// Compile with --strict
interface IFoo {
    a?: number[],
}

let foo: IFoo = {
    a: []
};

// error: Object is possible 'undefined'
foo.a.push(4);

// this will work
// foo.a = [];
// foo.a.push(5) // no errors
// alternatively,
foo!.a.push(3);

Expected behavior:
The compiler should be able to infer using the object initialization that foo.a is defined.

Actual behavior:
The compiler ignores the initialization of property 'a'.

Playground Link: Playground link (use strict check in options)

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions