Skip to content

Commit

Permalink
fix(flatMap): should infer return type correctly without depth prop (#…
Browse files Browse the repository at this point in the history
…809)

* fix(flatMap): should infer return type correctly without depth prop

* fix
  • Loading branch information
nnnnoel authored Nov 12, 2024
1 parent d76172a commit 674a795
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@
"format": "prettier --write .",
"transform": "jscodeshift -t ./.scripts/tests/transform-lodash-test.ts $0 && prettier --write $0"
}
}
}
7 changes: 7 additions & 0 deletions src/array/flatMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ describe('flatMap', () => {
const result = flatMap(emptyArr, item => [item, item]);
expect(result).toEqual([]);
});

it('should return type is flatten array without depth prop', () => {
const arr: string[] = ['1', '2'];
const result = flatMap(arr, item => [item, item]);
expect(result).toEqual(['1', '1', '2', '2']);
result[0].substring(0, 1);
});
});
4 changes: 2 additions & 2 deletions src/array/flatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import { flatten } from './flatten.ts';
* flatMap(arr, (item: number) => [[item, item]], 2);
* // [1, 1, 2, 2, 3, 3]
*/
export function flatMap<T, U, D extends number>(
export function flatMap<T, U, D extends number = 1>(
arr: readonly T[],
iteratee: (item: T) => U,
depth = 1 as D
depth: D = 1 as D
): Array<FlatArray<U[], D>> {
return flatten(
arr.map(item => iteratee(item)),
Expand Down

0 comments on commit 674a795

Please sign in to comment.