Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b171893

Browse filesBrowse files
authored
fix(computedAsync): return ComputedRef<T> type when lazy: true (#4751)
1 parent b1bc804 commit b171893
Copy full SHA for b171893

File tree

2 files changed

+25
-5
lines changed
Filter options

2 files changed

+25
-5
lines changed

‎packages/core/computedAsync/index.test.ts

Copy file name to clipboardExpand all lines: packages/core/computedAsync/index.test.ts
+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Ref } from 'vue'
1+
import type { ComputedRef, Ref } from 'vue'
22
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
33
import { computed, nextTick, shallowRef } from 'vue'
44
import { asyncComputed, computedAsync } from './index'
@@ -45,6 +45,16 @@ describe('computedAsync', () => {
4545
expectTypeOf(data2).toEqualTypeOf<Ref<string>>()
4646
})
4747

48+
it('types are correct when lazy', async () => {
49+
const func = vi.fn(() => Promise.resolve('data'))
50+
51+
const data1 = computedAsync(func, undefined, { lazy: true })
52+
const data2 = computedAsync(func, 'initialState', { lazy: true })
53+
54+
expectTypeOf(data1).toEqualTypeOf<ComputedRef<string | undefined>>()
55+
expectTypeOf(data2).toEqualTypeOf<ComputedRef<string>>()
56+
})
57+
4858
it('call onError when error is thrown', async () => {
4959
const errorMessage = shallowRef()
5060
const func = vi.fn(async () => {

‎packages/core/computedAsync/index.ts

Copy file name to clipboardExpand all lines: packages/core/computedAsync/index.ts
+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Fn } from '@vueuse/shared'
2-
import type { Ref } from 'vue'
2+
import type { ComputedRef, Ref } from 'vue'
33
import { noop } from '@vueuse/shared'
44
import {
55
computed,
@@ -63,18 +63,28 @@ export interface AsyncComputedOptions {
6363
export function computedAsync<T>(
6464
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
6565
initialState: T,
66-
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
66+
optionsOrRef: AsyncComputedOptions & { lazy: true },
67+
): ComputedRef<T>
68+
export function computedAsync<T>(
69+
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
70+
initialState: undefined,
71+
optionsOrRef: AsyncComputedOptions & { lazy: true },
72+
): ComputedRef<T | undefined>
73+
export function computedAsync<T>(
74+
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
75+
initialState: T,
76+
optionsOrRef?: Ref<boolean> | (AsyncComputedOptions & { lazy?: false | undefined }),
6777
): Ref<T>
6878
export function computedAsync<T>(
6979
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
7080
initialState?: undefined,
71-
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
81+
optionsOrRef?: Ref<boolean> | (AsyncComputedOptions & { lazy?: false | undefined }),
7282
): Ref<T | undefined>
7383
export function computedAsync<T>(
7484
evaluationCallback: (onCancel: AsyncComputedOnCancel) => T | Promise<T>,
7585
initialState?: T,
7686
optionsOrRef?: Ref<boolean> | AsyncComputedOptions,
77-
): Ref<T> | Ref<T | undefined> {
87+
): Ref<T> | Ref<T | undefined> | ComputedRef<T> | ComputedRef<T | undefined> {
7888
let options: AsyncComputedOptions
7989

8090
if (isRef(optionsOrRef)) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.