Microsoft/TypeScript

Nested generic calls don't inherit outer inferences when their return types are intersections containing functions

开放

#58,833 创建于 2024年6月12日

 (0 条评论) (1 个反应) (0 位负责人)TypeScript (13,395 个派生)batch import
Domain: IntersectionHelp WantedPossible Improvement

仓库指标

星标
 (108,860 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

🔎 Search Terms

generic call inference intersection return type

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240611#code/JYOwLgpgTgZghgYwgAgMIHsQ2AcwDwAqAfMgN4CwAUMsgppAB5gBcyBA3FTaAG7oDWEVhWo1kACgasCASlZ9gAE06iaEBhAStJ0mcgC8JBcq7IAvirNUqizQBs4UFDACuIBGGCZaTuJEJE4nRYuKwYIfjEcsjGKjb2js5uHl4gyAC2AJ4AkiB8ggBCUAIQIAHiwMLI6praUmx6hjHoSubRIjQ6DfItJpRmyABkyGCZAA4Q6DDIwHGUCL6Q4h209OosZKtuGwDM5gA0prwlrFm5+RBFJSDLpmoaWhIMjSQrYsgMd2IA9N-IAHoAfi+NAYADo6NswWB0AAxYAaRTiABMMhU7zMh1EZhkWJxc1sCAcTmQrncnm8ZzyJXKlU2NUeXVkBiMvTawlMTOisVMDLquhZzSUljmCwgfggt1EwUYG1IW3ArD2mKO1MEpxyaslb2qDzqLzIII+Rt+AOBqjE4Mh4GhcIRECRqPRYhV2NxVHxVCAA

💻 Code

interface Config<T> {
  context: T;
  invoke: {
    (x: T): void;
    exec: (x: T) => void;
  };
}

declare function create<T>(config: Config<T>): void;

declare function myInvokeBroken<T>(i: { exec: (x: T) => void }): {
  (x: T): void;
} & typeof i;

create({
  context: { count: 3 },
  invoke: myInvokeBroken({
    exec: (x) => {
      x
      // ^?
      x.count.toFixed(2);
    },
  }),
});

declare function myInvoke<T>(i: { exec: (x: T) => void }): {
  (x: T): void;
  exec: (x: T) => void;
};

create({
  context: { count: 3 },
  invoke: myInvoke({
    exec: (x) => {
      x
      // ^?
      x.count.toFixed(2);
    },
  }),
});

🙁 Actual behavior

x doesn't get contextually typed based on the outer inferences in the myInvokeBroken example

🙂 Expected behavior

I would expect myInvokeBroken and myInvoke to work here in the same way. Their return types are the same. It's only that myInvokeBroken is using an intersection in its return type while myInvoke isn't

Additional information about the issue

No response

贡献者指南