Closed
Description
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('');