Skip to content

Uninitialized variables work around strictNullChecks #13884

Closed
@rraval

Description

@rraval

TypeScript Version: 2.2.0

--strictNullChecks must be used.

Code

interface Foo {
    name: () => string;
}

let foo: Foo;
setTimeout((() => foo.name()), 0);

Expected behavior:

Error at let foo: Foo line since the compiler is implicitly doing let foo: Foo = undefined;.

If I were to write the = undefined initializer myself, the compiler properly errors with:

Type 'undefined' is not assignable to type 'Foo'.

Actual behavior:

The code compiles and errors at runtime:

TypeError: foo is undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions