Skip to content

Commit ba20929

Browse files
jh-leongyyx990803
andauthored
refactor(types/ref): update MaybeRef to include all ref-like types (#11379)
Co-authored-by: Evan You <[email protected]>
1 parent d6af694 commit ba20929

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

packages/dts-test/ref.test-d.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type Ref,
66
type ShallowRef,
77
type ToRefs,
8+
type WritableComputedRef,
89
computed,
910
isRef,
1011
proxyRefs,
@@ -465,8 +466,21 @@ describe('toRef <-> toValue', () => {
465466
})
466467

467468
// unref
468-
declare const text: ShallowRef<string> | ComputedRef<string> | MaybeRef<string>
469-
expectType<string>(unref(text))
469+
// #8747
470+
declare const unref1: number | Ref<number> | ComputedRef<number>
471+
expectType<number>(unref(unref1))
472+
473+
// #11356
474+
declare const unref2:
475+
| MaybeRef<string>
476+
| ShallowRef<string>
477+
| ComputedRef<string>
478+
| WritableComputedRef<string>
479+
expectType<string>(unref(unref2))
480+
481+
// toValue
482+
expectType<number>(toValue(unref1))
483+
expectType<string>(toValue(unref2))
470484

471485
// useTemplateRef
472486
const tRef = useTemplateRef('foo')

packages/reactivity/src/ref.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
toRaw,
1717
toReactive,
1818
} from './reactive'
19-
import type { ComputedRef } from './computed'
19+
import type { ComputedRef, WritableComputedRef } from './computed'
2020
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
2121
import { warn } from './warning'
2222

@@ -192,8 +192,13 @@ export function triggerRef(ref: Ref) {
192192
}
193193
}
194194

195-
export type MaybeRef<T = any> = T | Ref<T>
196-
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
195+
export type MaybeRef<T = any> =
196+
| T
197+
| Ref<T>
198+
| ShallowRef<T>
199+
| WritableComputedRef<T>
200+
201+
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T)
197202

198203
/**
199204
* Returns the inner value if the argument is a ref, otherwise return the
@@ -211,7 +216,7 @@ export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
211216
* @param ref - Ref or plain value to be converted into the plain value.
212217
* @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
213218
*/
214-
export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<T>): T {
219+
export function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T {
215220
return isRef(ref) ? ref.value : ref
216221
}
217222

@@ -231,9 +236,7 @@ export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<T>): T {
231236
* @param source - A getter, an existing ref, or a non-function value.
232237
* @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
233238
*/
234-
export function toValue<T>(
235-
source: MaybeRefOrGetter<T> | ComputedRef<T> | ShallowRef<T>,
236-
): T {
239+
export function toValue<T>(source: MaybeRefOrGetter<T>): T {
237240
return isFunction(source) ? source() : unref(source)
238241
}
239242

0 commit comments

Comments
 (0)