Skip to content

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

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

Closed
an5rag opened this issue Apr 30, 2018 · 1 comment
Closed

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

an5rag opened this issue Apr 30, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@an5rag
Copy link

an5rag commented Apr 30, 2018

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:

@mhegazy
Copy link
Contributor

mhegazy commented Apr 30, 2018

Looks like a duplicate of #20219, should be fixed by #22006

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 30, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants