Я написал функцию для получения первых n
элементов из массива:
export const first = <T>(array: T[], n?: number): T[] => {
if (n) return array.slice(0, n)
return [].concat(array).shift()
}
Вот ссылка скрипта для воспроизведения.
Компилятор возвращает ошибку
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()
Когда я проверяю lib.es5.d.ts
файл с VSCode, concat
принимает два параметра перегрузки.
concat(...items: ConcatArray<T>[]): T[];
concat(...items: (T | ConcatArray<T>)[]): T[];
Увидев второй тип вызова, Я попытался изменить первый параметр функции first
с 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>'
Что я должен сделать, чтобы исправить эти ошибки? Я новичок в TypeScript, так что извините, если это глупый вопрос.