Skip to content

TypeScript 2.2.1 generates surprising null checking error #14487

Closed
@BurtHarris

Description

@BurtHarris

TypeScript Version: 2.2.1

Effective type of argument p below seems to not match explicit declaration under tsc 2.2.1

Code

interface RuleContext  {
    _parent: RuleContext | undefined;
};

var  _ctx = { _parent: undefined};

function walk( p: RuleContext | undefined = _ctx ) {
    while (p) {
        p = p._parent; // Error w/ "strictNullChecks": true !
    }
}

Expected behavior:
Compiled w/o error under 2.1
Actual behavior:
Generates error:

index.ts(9,4): error TS2322: Type 'RuleContext | undefined' is not assignable to type 'RuleContext'.
  Type 'undefined' is not assignable to type 'RuleContext'.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions