简体   繁体   English

为什么我在使用数组 concat 时不断收到 TypeScript 错误“没有重载匹配此调用”?

[英]Why am I keep getting TypeScript error “No overload matches this call” when using Array concat?

I've written a function to get first n items from an array:我写了一个 function 从数组中获取前n项目:

export const first = <T>(array: T[], n?: number): T[] => {
  if (n) return array.slice(0, n)
  return [].concat(array).shift()
}

Here is the link of the script to reproduce.这是要重现的脚本的链接

The compiler returns an error编译器返回错误

error TS2769: No overload matches this call.
      Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
        Argument of type 'T[]' is not assignable to parameter of type 'ConcatArray<never>'.
          The types returned by 'slice(...)' are incompatible between these types.
            Type 'T[]' is not assignable to type 'never[]'.
              Type 'T' is not assignable to type 'never'.
      Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
        Argument of type 'T[]' is not assignable to parameter of type 'ConcatArray<never>'.

   return [].concat(array).shift()

When I check lib.es5.d.ts file with VSCode, concat accepts two overloading parameters.当我用 VSCode 检查lib.es5.d.ts文件时, concat接受两个重载参数。

concat(...items: ConcatArray<T>[]): T[];
concat(...items: (T | ConcatArray<T>)[]): T[];

By seeing the second type of call, I've tried to change the first parameter of first function from array: T[] to array: (T | ConcatArray<T>)[] but then it returns error,通过查看第二种类型的调用,我尝试将第first function 的第一个参数从array: T[]更改为array: (T | ConcatArray<T>)[]但随后它返回错误,

Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
    Argument of type '(T | ConcatArray<T>)[]' is not assignable to parameter of type 'ConcatArray<never>'

What should I do to fix those errors?我应该怎么做才能修复这些错误? I'm new to TypeScript so pardon me if this is a dumb question.我是 TypeScript 的新手,如果这是一个愚蠢的问题,请原谅我。

In this github issue you can see why the array have a never type ,the principal response is:在这个 github 问题中,您可以看到为什么数组有 never 类型,主要响应是:

This is caused by the combination of strict and noImplicitAny: false.这是由 strict 和 noImplicitAny: false 的组合引起的。 In general we expect that if strict is on, noImplicitAny is also on;一般来说,我们希望如果 strict 开启,则 noImplicitAny 也开启; this particular set of settings will expose some odd behavior.这组特定的设置会暴露一些奇怪的行为。 If you had both on, you'd see an error about the [] being implicitly any[];如果两者都打开,您会看到关于 [] 隐含为 any[]; 的错误。 if both were off;如果两者都关闭; we'd use control flow analysis and treat the array as a number[] after the push(1);.我们将使用控制流分析并将数组视为 push(1); 之后的数字[]。

The particular combination of settings means that we don't allow ourselves to use control flow analysis or allow the array to be implicitly any[], so never[] is the only remaining allowable option.设置的特定组合意味着我们不允许自己使用控制流分析或允许数组隐含为 any[],因此 never[] 是唯一剩下的允许选项。

I'd recommend turning off strict if you're not going to have noImplicitAny on.如果您不打算启用 noImplicitAny,我建议您关闭 strict。

respect to how to fix your problem, you can use any of the two approaches I write in this TS playground , I recommend you the second because there you will take in account the two possible values returned by the function, in the first you just cheat TS with the type assertion to return T[], but probably the type will be undefined when the array doesn't have a value.关于如何解决您的问题,您可以使用我在此 TS 操场上编写的两种方法中的任何一种,我建议您使用第二种方法,因为您将考虑 function 返回的两个可能值,首先您只是作弊TS 带有返回 T[] 的类型断言,但是当数组没有值时,类型可能是未定义的。

const second = <T>(array: T[], n?: number): (T | undefined)[] => {
  if (n) return array.slice(0, 2)

  return [array.shift()]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是打字稿的新手当我编译我的代码时,没有重载匹配这个调用错误 - I am new in typescript When I compile my code given No overload matches this call error expressJS 的 TypeScript 错误:没有与此调用匹配的重载 - TypeScript Error for expressJS: No overload matches this call Typescript 路由错误 - 没有与此调用匹配的过载 - Typescript Routing Error - No overload matches this call NodeJS“没有重载匹配这个调用。” 打字稿错误 - NodeJS "No overload matches this call." error with typescript 没有重载匹配这个调用。 最后一次重载在打字稿中出现以下错误 - No overload matches this call. The last overload gave the following error in typescript 当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。 - when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.” Typescript 使用 react-daterange-picker 时出错“没有重载匹配此调用。” - Typescript Error using react-daterange-picker "No overload matches this call." vue watch TypeScript 错误TS2769: No overload matches this call - Vue watch TypeScript error TS2769: No overload matches this call 调用 axios 时出现 Typescript 错误 - 没有与此调用匹配的过载 - Typescript error while calling axios - no overload matches this call clearInterval() 在 typescript 中使用时抛出“没有重载匹配此调用” - clearInterval() throwing 'No overload matches this call' when used in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM