feat(collections): improve types of partition module (#2744)

add type guard overload
This commit is contained in:
Satoshi 2022-10-05 00:18:53 +09:00 committed by GitHub
parent 7a7385dfe0
commit 7f7583139e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,12 +18,20 @@
* assertEquals(odd, [ 5, 7, 9 ])
* ```
*/
export function partition<T, U extends T>(
array: readonly T[],
predicate: (el: T) => el is U,
): [U[], Exclude<T, U>[]];
export function partition<T>(
array: readonly T[],
predicate: (el: T) => boolean,
): [T[], T[]] {
const matches: Array<T> = [];
const rest: Array<T> = [];
): [T[], T[]];
export function partition(
array: readonly unknown[],
predicate: (el: unknown) => boolean,
): [unknown[], unknown[]] {
const matches: Array<unknown> = [];
const rest: Array<unknown> = [];
for (const element of array) {
if (predicate(element)) {