Microsoft/TypeScript

No type inferrence of callback arguments inside a tuple union type

Open

#55,632 创建于 2023年9月5日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 Search Terms

  • discriminated union inference
  • union argument inference

🕗 Version & Regression Information

  • This is a compilation error in tsserver 3.3.2

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgdgMwgJwILIOZQLwCgpQA+UA2gIwA0UAFAIYBcUcArgLYBGKAlDgHxQA3APYBLACYBdfEVIAmKtXaMAzsGQi4GHtn7DxEgNy4AxkLiqotTI3hI0mHKUo1a2-gG8oAX0O4gA

💻 Code

type InferArg =
  | [1, (a: number) => void]
  | [2, (b: string) => void];
const arg: InferArg = [1, (a) => { }];

🙁 Actual behavior

I get a compilation error:

Parameter 'a' implicitly has an 'any' type.(7006)

🙂 Expected behavior

a should be inferred to be a number because I already passed 1 as the first tuple element. Type inference should be identical to this code:

type InferArg = [1, (a: number) => void];
const arg: InferArg = [1, (a) => { }];

Additional information about the issue

No response

贡献者指南