Microsoft/TypeScript

A tuple containing a spread not at the trailing edge has its minimum length calculated incorrectly

Open

#52,688 建立於 2023年2月8日

在 GitHub 查看
 (2 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

🔎 Search Terms

  • tuple
  • spread

🕗 Version & Regression Information

It happens in v4.9.5, no idea about other versions.

⏯ Playground Link

https://www.typescriptlang.org/play?jsx=0#code/C4TwDgpgBAYgcgRigHgCoD4oF4oAoB0hAhgE4DmAzgFxQXAkCWAdmQNoC6AlNpqgNwAoAaEiw4AJhQZseKIXylKNVgGMA9kwoBXALYQAJjTqMWAGjmEyJNVrDVa9Zm3bnmYLcCOOzUZvogAHl4mZOxQ3Fi8ggLqmsBQAGZMCDTwCMhMugBGECSYOLhQROZZ4TxQAAzRsXSJTOKpEhnZufmyxVClEZhVQA

💻 Code

type FN1 <T> = (...args: string[]) => T;

type FN2 <T> = ( ...args: [consumed: string, ...groups: string[], input: string, index: string] ) => T;

const fn1: FN1<number> = ( a, b ) => 0;

const fn2: FN2<number> = ( a, b ) => 0;

🙁 Actual behavior

TS complains about my fn2, with the following error:

Type '(a: string, b: string) => number' is not assignable to type 'FN2<number>'.
  Types of parameters 'b' and 'groups' are incompatible.
    Type '[...groups: string[], input: string, index: string]' is not assignable to type '[b: string]'.
      Source has 2 element(s) but target allows only 1.(2322)

If I'm interpreting it correctly it is saying like "hey you defined two arguments, but maybe this tuple only has one item in it.

🙂 Expected behavior

If I'm interpreting the error correctly then the error is wrong because there's no scenario where that tuple has just one item in it, minimum it will always have 3.

貢獻者指南