Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object literal contextual typing where 'this' has type 'any' in lambda functions #8368

Closed
kitsonk opened this issue Apr 29, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: This-Typing The issue relates to providing types to this

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Apr 29, 2016

TypeScript Version:

nightly (1.9.0-dev.20160429)

Code

interface Factory<T> {
    (): T;
    mixin<U>(mixin: U): T & U;
}

function compose<T>(base: T): Factory<T> {
    return;
}

const factory = compose({
    foo() {
        return this;
    },
    bar() {
        this.foo(); // <-- properly contextual and literal union type
        return () => {
            return this; // <-- type `any`, but should be same as outside the lambda
        };
    }
});

const foobar = {
    foo() {
        return this;
    },
    bar() {
        return () => {
            return this; // <-- correct object literal type
        };
    }
};

Expected behavior:

That both references to this are typed the same.

Actual behavior:

I am pretty sure this is related to #8356.

Outside of the lambda function in the first object literal, this is an intersection type of the object literal and the contextual type (which happen to be the same). In the second example, this is properly the object literal type including within the scope of the lambda function.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Apr 29, 2016
@DanielRosenwasser DanielRosenwasser changed the title Object literal contextual typing this widening to any in lambda functions Object literal contextual typing where 'this' has type 'any' in lambda functions Apr 29, 2016
@DanielRosenwasser DanielRosenwasser added the Domain: This-Typing The issue relates to providing types to this label Apr 29, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 30, 2016

should be fixed by #8389

@mhegazy mhegazy added this to the TypeScript 2.0 milestone Apr 30, 2016
@sandersn
Copy link
Member

#8389 is in -- it fixes this bug by making both references to this be of type any. Which is technically the same type, but not a useful one. We decided that we couldn't do the right thing for the majority of uses of this in object literals, like those in this bug and #8191.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: This-Typing The issue relates to providing types to this
Projects
None yet
Development

No branches or pull requests

4 participants