kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
https://npm.im/match-sorter
MIT License
3.73k stars 129 forks source link

`matchSorter` does not accept readonly arrays. #128

Closed diesieben07 closed 3 years ago

diesieben07 commented 3 years ago
const input: readonly string[] = ['foo', 'bar'];
const matchSorted = matchSorter(input, 'f'); // does not compile, plain array is required

Problem description: The type signature of matchSorter is overly restrictive. Even though it does not modify its input array, it does not accept readonly arrays. To work around this one needs to either do a cast (which basically disables type safety) or copy the array unnecessarily before passing it to matchSorter.

Suggested solution: matchSorter should accept a ReadonlyArray instead of a plain Array.

I am creating this issue before a pull request, to first have a discussion about the scope. This readonly issue not only affects the input array (although that is where I discovered it), it also applies to for example MatchSorterOptions.keys. Changing it there however would be a breaking change (as opposed to the type signature of matchSorter).

kentcdodds commented 3 years ago

I think we should support a regular array as well as a readonly array. PR is welcome.

diesieben07 commented 3 years ago

A regular array can also be passed if a readonly array is expected. The following compiles fine:

function acceptsArray(array: ReadonlyArray<string>) { }

const plainArray: string[] = ['foo'];
acceptsArray(plainArray); // compiles!

I'll make a PR later today!

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 6.3.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: