microsoft / microsoft/TypeScript

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

Open
#28,159 1 comment 0 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Dec 8, 2018.

Bug Domain: This-Typing
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.