microsoft / microsoft/TypeScript

3.6 regression: unions of callable types involving `this` types

Open
#33,379 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: Version 3.6.3, Version 3.7.0-dev.20190911

Search Terms: intersect function

Code

class A {
    public uniqueToA = "a";

    public getString(this: A): string {
        return "a";
    }
}

class B {
    public uniqueToB = "b";

    public getString(this: B): string {
        return "b";
    }
}

function f(model: A | B): string {
    return model.getString();
}

Expected behavior:

In Typescript 3.5.3, the model.getString() in function f has no error.

Actual behavior:

In Typescript 3.6.3 and 3.7.0-dev.20190911, the code above gets the following errors:

index.ts:18:12 - error TS2684: The 'this' context of type 'A | B' is not assignable to method's 'this' of type 'A & B'.
  Type 'A' is not assignable to type 'A & B'.
    Property 'uniqueToB' is missing in type 'A' but required in type 'B'.

18     return model.getString();
              ~~~~~

  index.ts:10:12
    10     public uniqueToB = "b";
                  ~~~~~~~~~
    'uniqueToB' is declared here.

Playground Link:
http://www.typescriptlang.org/play/#code/MYGwhgzhAECC0G8BQ1XQA4FcBGICWw0mAdngI6YCmAKgPbwC80ARGMwNxIppa4HQBzSgBcAysIBOeYgIAUwgBZ4IALjgBKNREnSBibmjQSRmCcRZtOhgL5JbSUJBgAhfYd75CJclTqumzNgcXO44noIi4lIy8kqq0M6a0NrResiGhsbCpuaBwTZ2XABmJMDCeLTmRbIAtrQAJpQgavAAPglJKbpuRiZm0HWNIAB0QmI6Meqc1kA

Related Issues: #32506

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.

Research direction

Reproduce the index.ts example with TypeScript 3.5.3, 3.6.3, and the referenced 3.7.0-dev version to confirm the regression. Trace the type-checking path for model.getString() and unions of callable types with this parameters; done means the example compiles without the TS2684 error while preserving the relevant this checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.