Skip to content

Narrowing breaking changes #7662

Closed
Closed
@mhegazy

Description

@mhegazy

This is a place holder for changes to the type narrowing behavior in TypeScript 2.0:

  • Generic type parameters are now narrowed
function g<T>(obj: T) {
    var t: T;
    if (obj instanceof RegExp) {
         t = obj; // RegExp is not assignable to T
    }
}

See the explanation by @Arnavion in #7719 (comment)

  • Type narrowing does not work for catprued variables in functions and class expressions
export class Class {
    constructor(public p) { }
}

var c: Class | string;
var x: Class;

if (c instanceof Class) {
    function inner() {
        x = c; // Error, type of c is not narrowed, c is Class | string
    }
    x = c; // OK, c is Class
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions