Skip to content

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

Open

Description

@SLaks

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

Activity

  1. weswigham commented on Oct 26, 2018

    @weswigham
    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

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: This-TypingThe issue relates to providing types to this

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions