Skip to content

Generic type defaulting to {} instead of any #7505

Closed
@robianmcd

Description

@robianmcd

TypeScript Version:
1.7.3

Code

window['test'] = {a:1, b:2};

function myFunc<T>(): T
{
    return window['test'];
}

let test = myFunc();
console.log(test.a);

Expected behavior:
The type of test is any so there is no compiler error.

Actual behavior:
I get the following error message
myFile.ts(9,18): error TS2339: Property 'a' does not exist on type '{}'.

Use Case
I run into this issue whenever I'm working on an Angular project and using the $http service. The type definition for $http.get() is as follows

get<T>(url: string, config?: IRequestShortcutConfig): IHttpPromise<T>;

so if you don't explicitly specify what T is you get an error when you try to access the response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions