Skip to content

feat(reactivity): improve support of getter usage in reactivity APIs #7997

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

Merged
merged 11 commits into from
Apr 2, 2023
Prev Previous commit
Next Next commit
feat(reactivity): toValue
  • Loading branch information
yyx990803 committed Mar 31, 2023
commit e4397e60270bff533457d25892a03cbdd07f1e96
18 changes: 10 additions & 8 deletions packages/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
reactive,
proxyRefs,
toRef,
toValue,
toRefs,
ToRefs,
shallowReactive,
readonly,
MaybeRef,
MaybeReadonlyRef
MaybeWritableRef
} from 'vue'
import { expectType, describe } from './utils'

Expand All @@ -28,7 +29,8 @@ function plainType(arg: number | Ref<number>) {

// ref unwrapping
expectType<number>(unref(arg))
expectType<number>(unref(() => 123))
expectType<number>(toValue(arg))
expectType<number>(toValue(() => 123))

// ref inner type should be unwrapped
const nestedRef = ref({
Expand Down Expand Up @@ -330,11 +332,11 @@ describe('reactive in shallow ref', () => {
expectType<number>(x.value.a.b)
})

describe('toRef <-> unref', () => {
describe('toRef <-> toValue', () => {
function foo(
a: MaybeRef<string>,
a: MaybeWritableRef<string>,
b: () => string,
c: MaybeReadonlyRef<string>
c: MaybeRef<string>
) {
const r = toRef(a)
expectType<Ref<string>>(r)
Expand All @@ -352,9 +354,9 @@ describe('toRef <-> unref', () => {
rc.value = 'foo'

return {
r: unref(r),
rb: unref(rb),
rc: unref(rc)
r: toValue(r),
rb: toValue(rb),
rc: toValue(rc)
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ export {
shallowRef,
isRef,
toRef,
toValue,
toRefs,
unref,
proxyRefs,
customRef,
triggerRef,
type Ref,
type MaybeRef,
type MaybeReadonlyRef,
type MaybeWritableRef,
type ToRef,
type ToRefs,
type UnwrapRef,
Expand Down
12 changes: 8 additions & 4 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function triggerRef(ref: Ref) {
triggerRefValue(ref, __DEV__ ? ref.value : void 0)
}

export type MaybeRef<T = any> = T | Ref<T>
export type MaybeReadonlyRef<T = any> = MaybeRef<T> | (() => T)
export type MaybeWritableRef<T = any> = T | Ref<T>
export type MaybeRef<T = any> = MaybeWritableRef<T> | (() => T)

/**
* Returns the inner value if the argument is a ref, otherwise return the
Expand All @@ -213,8 +213,12 @@ export type MaybeReadonlyRef<T = any> = MaybeRef<T> | (() => T)
* @param ref - Ref or plain value to be converted into the plain value.
* @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
*/
export function unref<T>(ref: MaybeReadonlyRef<T>): T {
return isRef(ref) ? (ref.value as any) : isFunction(ref) ? ref() : ref
export function unref<T>(ref: MaybeWritableRef<T>): T {
return isRef(ref) ? (ref.value as any) : ref
}

export function toValue<T>(ref: MaybeRef<T>): T {
return isFunction(ref) ? ref() : unref(ref)
}

const shallowUnwrapHandlers: ProxyHandler<any> = {
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
proxyRefs,
isRef,
toRef,
toValue,
toRefs,
isProxy,
isReactive,
Expand Down Expand Up @@ -153,7 +154,7 @@ export { TrackOpTypes, TriggerOpTypes } from '@vue/reactivity'
export type {
Ref,
MaybeRef,
MaybeReadonlyRef,
MaybeWritableRef,
ToRef,
ToRefs,
UnwrapRef,
Expand Down