Skip to content

Commit d4c6957

Browse files
committed
fix(types): ref value type unwrapping should happen at creation time
1 parent 711d16c commit d4c6957

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

packages/reactivity/src/computed.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { effect, ReactiveEffect, trigger, track } from './effect'
22
import { TriggerOpTypes, TrackOpTypes } from './operations'
3-
import { Ref, UnwrapRef } from './ref'
3+
import { Ref } from './ref'
44
import { isFunction, NOOP } from '@vue/shared'
55

66
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
7-
readonly value: UnwrapRef<T>
7+
readonly value: T
88
}
99

1010
export interface WritableComputedRef<T> extends Ref<T> {

packages/reactivity/src/ref.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface Ref<T = any> {
1717
// don't want this internal field to leak into userland autocompletion -
1818
// a private symbol, on the other hand, achieves just that.
1919
[isRefSymbol]: true
20-
value: UnwrapRef<T>
20+
value: T
2121
}
2222

2323
const convert = <T extends unknown>(val: T): T =>
@@ -28,13 +28,13 @@ export function isRef(r: any): r is Ref {
2828
return r ? r._isRef === true : false
2929
}
3030

31-
export function ref<T>(value: T): T extends Ref ? T : Ref<T>
31+
export function ref<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>
3232
export function ref<T = any>(): Ref<T>
3333
export function ref(value?: unknown) {
3434
return createRef(value)
3535
}
3636

37-
export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
37+
export function shallowRef<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>
3838
export function shallowRef<T = any>(): Ref<T>
3939
export function shallowRef(value?: unknown) {
4040
return createRef(value, true)

test-dts/ref.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ function foo(arg: number | Ref<number>) {
1313

1414
// ref unwrapping
1515
expectType<number>(unref(arg))
16+
17+
// ref inner type should be unwrapped
18+
const nestedRef = ref({
19+
foo: ref(1)
20+
})
21+
expectType<Ref<{ foo: number }>>(nestedRef)
1622
}
1723

1824
foo(1)

test-dts/watch.test-d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ watch(
5252
},
5353
{ immediate: true }
5454
)
55+
56+
// should provide correct ref.value inner type to callbacks
57+
const nestedRefSource = ref({
58+
foo: ref(1)
59+
})
60+
61+
watch(nestedRefSource, (v, ov) => {
62+
expectType<{ foo: number }>(v)
63+
expectType<{ foo: number }>(ov)
64+
})

0 commit comments

Comments
 (0)