小编Iho*_*hor的帖子

flatten 函数的 TypeScript 类型定义

我有简单的flatten功能。这个想法是它可以采用字符串数组或字符串数​​组数组,并且始终只返回 1 级字符串数组。例如:

flatten(['a', ['b']]) // returns ['a', 'b']
flatten(['a', 'b']) // returns ['a', 'b']
Run Code Online (Sandbox Code Playgroud)

下面是这个函数的实现

function flatten(arr: ReadonlyArray<string | string[]>): string[] {
   return [].concat(...arr);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下 TypeScript 编译器错误:

  error TS2769: No overload matches this call.
    Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
      Argument of type 'string | string[]' is not assignable to parameter of type 'ConcatArray<never>'.
        Type 'string' is not assignable to type 'ConcatArray<never>'.
    Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the …
Run Code Online (Sandbox Code Playgroud)

types typescript typescript-typings

1
推荐指数
1
解决办法
679
查看次数

标签 统计

types ×1

typescript ×1

typescript-typings ×1