Skip to content

strictNullChecks mode - Object is possibly 'undefined'. #10642

Closed
@aliai

Description

@aliai

In strictNullChecks mode only:

TypeScript Version: nightly (2.0.2)

Code

interface IRoute {
  children?: IRoute[];
}

function register(route: IRoute) {
  if (route.children) {                                   // <-- route.children is of type IRoute|undefined
    Object.keys(route.children).forEach(routeKey => {     // <-- route.children is of type IRoute
      register(route.children[routeKey]);  // <-- Error: Object 'route.children' is possibly 'undefined'.
    });
  }
}

Expected behavior:

There shouldn't be an error here, because route.children is being checked 2 lines above.

Actual behavior:

TypeScript fails to recognize route.children cannot be undefined anymore, because of the nested function. It is the same behavior with both function declaration and arrow functions.

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