microsoft / microsoft/TypeScript

Type variable not inferred correctly unless unused declaration is provided

Open
#52,432 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

The described bug was noticed during debugging of the declarations provided with the Vue framework.

🔎 Search Terms

generic, inference, function, overload

🕗 Version & Regression Information

All 4.x versions available on the playground.

⏯ Playground Link

Playground link with relevant code

💻 Code
interface Methods {
  [key: string]: Function;
}

interface Param<M extends Methods> {
  data: (this: M) => any;
  methods: M;
}

declare function fun<M extends Methods = {}>(
  param: Param<M> & ThisType<M>
): void;

// Uncommenting the declaration below results in proper type inference of M

// declare function fun<M extends Methods = {}>(
//   param: { unusedProperty: true } & Param<M> & ThisType<M>
// ): void;

fun({
  data() {
    this.myMethod(); // Even after uncommenting the overloaded version, context is not properly inferred here
  },
  methods: {
    myMethod() {},
    mySecondMethod() {
      this.myMethod(); // Error without uncommenting the declaration
    }
  },
});
🙁 Actual behavior
  1. Proper inference of the M type variable and this context in the methods section depends on having an unused overloaded version of a function.
  2. Even though M is properly inferred for fun (visible after hovering over the function) and this context in methods after uncommenting the unused declaration, this context in data is still not properly inferred (when hovering over this, it looks as it is inferred to the default {}).
🙂 Expected behavior

M type variable is properly inferred in both data function and methods section without redundant declarations.

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

Start by running the linked TypeScript Playground example and compare inference for the two declarations of fun, focusing on the generic M and the this contexts in data and methods. No repository file or test is identified in the issue, so locate the compiler inference and contextual-typing paths before adding a regression case; done means both contexts infer M without the redundant overload.

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.