Microsoft/TypeScript

Type variable not inferred correctly unless unused declaration is provided

開放

#52,432 建立於 2023年1月26日

 (1 則留言) (0 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
BugDomain: check: Type InferenceHelp Wanted

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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.

貢獻者指南