Skip to content

Generic symbols without an actual generic type now 'infer' {} instead of anyย #10990

Closed
@dbaeumer

Description

@dbaeumer

TypeScript Version: 2.0.2
Code

function bug<T>(obj: T): T {
    if (obj instanceof RegExp) {
        return obj;
    }
    return null;
}

function isArray<T>(arg: any): arg is Array<T> {
    return true;
}

function bug2() {
    let rules: any;

    if (isArray(rules)) {
        let str: string = rules[0];
    }
}

function bug3() {
    let values: any[];
    let strings: string[];

    if (isArray(values)) {
        strings = values;
    }
}

All this code worked in 1.8.10 but now fails in 2.0.2. From what I can read into the error message the taken type when no actual type parameter is provided is now {}. It seemed to was any before. If I change the code to for example

function bug2() {
    let rules: any;

    if (isArray<any>(rules)) {
        let str: string = rules[0];
    }
}

Everything compiles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptVS Code TrackedThere is a VS Code equivalent to this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions