Skip to content

Commit 45b187a

Browse files
authored
fix(computedAsync): type signature (#4207)
Signed-off-by: GitHub <noreply@github.com>
1 parent e71eb1e commit 45b187a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/core/computedAsync/index.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { Ref } from 'vue-demi'
12
import { computed, nextTick, ref } from 'vue-demi'
23
import { promiseTimeout } from '@vueuse/shared'
3-
import { describe, expect, it, vi } from 'vitest'
4+
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
45
import { asyncComputed, computedAsync } from '.'
56

67
describe('computed', () => {
@@ -37,6 +38,16 @@ describe('computedAsync', () => {
3738
expect(data.value).toBe('data')
3839
})
3940

41+
it('types are correct', async () => {
42+
const func = vi.fn(() => Promise.resolve('data'))
43+
44+
const data1 = computedAsync(func)
45+
const data2 = computedAsync(func, 'initialState')
46+
47+
expectTypeOf(data1).toEqualTypeOf<Ref<string | undefined>>()
48+
expectTypeOf(data2).toEqualTypeOf<Ref<string>>()
49+
})
50+
4051
it('call onError when error is thrown', async () => {
4152
let errorMessage
4253
const func = vi.fn(async () => {
@@ -180,7 +191,7 @@ describe('computedAsync', () => {
180191
return Promise.resolve(result)
181192
})
182193
const other = computed(() => {
183-
return double.value + 1
194+
return (double.value ?? 0) + 1
184195
})
185196

186197
expect(double.value).toBeUndefined()

packages/core/computedAsync/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,21 @@ export interface AsyncComputedOptions {
4444
* @param initialState The initial state, used until the first evaluation finishes
4545
* @param optionsOrRef Additional options or a ref passed to receive the updates of the async evaluation
4646
*/
47+
export function computedAsync<T>(
48+
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
49+
initialState: T,
50+
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
51+
): Ref<T>
52+
export function computedAsync<T>(
53+
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
54+
initialState?: undefined,
55+
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
56+
): Ref<T | undefined>
4757
export function computedAsync<T>(
4858
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
4959
initialState?: T,
5060
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
51-
): Ref<T> {
61+
): Ref<T> | Ref<T | undefined> {
5262
let options: AsyncComputedOptions
5363

5464
if (isRef(optionsOrRef)) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy