Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useArrayMap): allow return type matches the mapper function #2172

Merged
merged 2 commits into from Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/shared/useArrayMap/index.test.ts
Expand Up @@ -26,4 +26,16 @@ describe('useArrayMap', () => {
list.value.pop()
expect(result.value).toStrictEqual([0, 2, 4, 6])
})

it('should match the return type of mapper function', () => {
const list = ref([0, 1, 2, 3])
const result1 = useArrayMap(list, i => i.toString())
result1.value.forEach(i => expect(i).toBeTypeOf('string'))

const result2 = useArrayMap(list, i => ({ value: i }))
result2.value.forEach((item, idx) => {
expect(item).toBeTypeOf('object')
expect(item).toHaveProperty('value', idx)
})
})
})
6 changes: 3 additions & 3 deletions packages/shared/useArrayMap/index.ts
Expand Up @@ -12,9 +12,9 @@ import { resolveUnref } from '../resolveUnref'
*
* @returns {Array} a new array with each element being the result of the callback function.
*/
export function useArrayMap<T>(
export function useArrayMap<T, U = T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
fn: (element: T, index: number, array: T[]) => T,
): ComputedRef<T[]> {
fn: (element: T, index: number, array: T[]) => U,
): ComputedRef<U[]> {
return computed(() => resolveUnref(list).map(i => resolveUnref(i)).map(fn))
}