Skip to content

@core-js/types pure - array methods don't work on ReadonlyArray #1532

Description

@DetachHead

for example with toSorted and toSpliced:

import toSpliced from '@core-js/pure/es/array/to-spliced';

declare const foo: ReadonlyArray<number>;

// error: Argument of type 'readonly number[]' is not assignable to parameter of type 'number[]'.
//   The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'
toSpliced(foo, 1);

in typescript/lib/lib.es2023.array.d.ts these methods are defined on ReadonlyArray:

interface ReadonlyArray<T> {
    /**
     * Copies and sorts the array.
     * @param compareFn Function used to determine the order of the elements. It is expected to return
     * a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
     * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
     * ```ts
     * [11, 2, 22, 1].toSorted((a, b) => a - b) // [1, 2, 11, 22]
     * ```
     */
    toSorted(compareFn?: (a: T, b: T) => number): T[];

    /**
     * Copies an array and removes elements while, if necessary, inserting new elements in their place, returning the remaining elements.
     * @param start The zero-based location in the array from which to start removing elements.
     * @param deleteCount The number of elements to remove.
     * @param items Elements to insert into the copied array in place of the deleted elements.
     * @returns A copy of the original array with the remaining elements.
     */
    toSpliced(start: number, deleteCount: number, ...items: T[]): T[];

    /**
     * Copies an array and removes elements while returning the remaining elements.
     * @param start The zero-based location in the array from which to start removing elements.
     * @param deleteCount The number of elements to remove.
     * @returns A copy of the original array with the remaining elements.
     */
    toSpliced(start: number, deleteCount?: number): T[];
}

but in @core-js/types/ts5-6/pure.d.ts their self type is Array<T>:

// @core-js/types/ts5-6/pure.d.ts

declare module '@core-js/pure/es/array/to-spliced' {
  const resultMethod: <T>(self: Array<T>, ...args: Parameters<CoreJS.ArrayToSpliced<T>>) => ReturnType<CoreJS.ArrayToSpliced<T>>;
  export = resultMethod;
}

they should be updated to self: ReadonlyArray<T> instead

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions