Skip to content

Type inference defaults to '{}' in call sites. #5254

Closed
@drarmstr

Description

@drarmstr

I'm using TypeScript 1.6.3 in Visual Studio 2015. The es6-promise declaration I'm using contains:

declare class Promise<R> implements Thenable<R> {
    constructor(callback: (resolve : (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => void);
    then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
}

The following works with an explicit typing of the Promise generic:

new Promise < { foo: number }>((resolve) => {
    resolve({ foo: 1 });
}).then((value) => {
    value.foo;
});

However, shouldn't type inference allow the following to work?

new Promise((resolve) => {
    resolve({ foo: 1 });
}).then((value) => {
    value.foo;
});

Instead, I am getting an error when accessing value.foo that foo does not exist on type '{}'. Why is TypeScript defaulting to the {} type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DiscussionIssues which may not have code impact

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions