【发布时间】:2020-06-04 01:34:33
【问题描述】:
我有简单的flatten 功能。这个想法是它可以采用字符串数组或字符串数组数组,并且总是只返回 1 级字符串数组。例如:
flatten(['a', ['b']]) // returns ['a', 'b']
flatten(['a', 'b']) // returns ['a', 'b']
这里是这个函数的实现
function flatten(arr: ReadonlyArray<string | string[]>): string[] {
return [].concat(...arr);
}
我收到以下 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 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>'.
105 return [].concat(...arr);
~~~~~~
如何定义此flatten 函数的输入和输出类型?我想避免使用any 类型。
【问题讨论】:
标签: typescript types typescript-typings