Skip to content

Bug: Chained this-intersecting methods don't work from within class #28159

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

Open
SLaks opened this issue Oct 26, 2018 · 1 comment
Open

Bug: Chained this-intersecting methods don't work from within class #28159

SLaks opened this issue Oct 26, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Domain: This-Typing The issue relates to providing types to this
Milestone

Comments

@SLaks
Copy link

SLaks commented Oct 26, 2018

TypeScript Version: 3.1.1-insiders.20180926

Search Terms: this chain

Code

class Builder {
    private _class: undefined;
    withFoo<T>() {
        return this as this & { foo: T }
    }
    withBar<T>() {
        return this as this & { bar: T }
    }
    withFooBar<T>() {
        return this.withFoo<T>().withBar<T>();
    }
}

let good = new Builder().withFoo<number>().withBar<number>();
let bad = new Builder().withFooBar<number>();
good = bad;

Expected behavior:

good & bad should be identical

Actual behavior:

bad is missing foo

Playground Link: http://www.typescriptlang.org/play/#src=class%20Builder%20%7B%0D%0A%20%20%20%20private%20_class%3A%20undefined%3B%0D%0A%20%20%20%20withFoo%3CT%3E()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20this%20as%20this%20%26%20%7B%20foo%3A%20T%20%7D%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20withBar%3CT%3E()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20this%20as%20this%20%26%20%7B%20bar%3A%20T%20%7D%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20withFooBar%3CT%3E()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20this.withFoo%3CT%3E().withBar%3CT%3E()%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20good%20%3D%20new%20Builder().withFoo%3Cnumber%3E().withBar%3Cnumber%3E()%3B%0D%0Alet%20bad%20%3D%20new%20Builder().withFooBar%3Cnumber%3E()%3B%0D%0Agood%20%3D%20bad%3B

@weswigham weswigham added Bug A bug in TypeScript Domain: This-Typing The issue relates to providing types to this labels Oct 26, 2018
@weswigham
Copy link
Member

Looking at the quick info for withFooBar, it would appear that we instantiate the this type for the withBar call in the method body with the class's this, rather than the call target (which would be this & {foo:T}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: This-Typing The issue relates to providing types to this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants