Skip to content

Commit c86e7ad

Browse files
authored
types(reactivity): improve typings for shallowRef (vuejs#1780)
1 parent 4172fdb commit c86e7ad

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/reactivity/src/ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export function ref(value?: unknown) {
3535
return createRef(value)
3636
}
3737

38-
export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
38+
export function shallowRef<T extends object>(
39+
value: T
40+
): T extends Ref ? T : Ref<T>
41+
export function shallowRef<T>(value: T): Ref<T>
3942
export function shallowRef<T = any>(): Ref<T | undefined>
4043
export function shallowRef(value?: unknown) {
4144
return createRef(value, true)

test-dts/ref.test-d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Ref,
33
ref,
4+
shallowRef,
45
isRef,
56
unref,
67
reactive,
@@ -120,6 +121,22 @@ const state = reactive({
120121

121122
expectType<string>(state.foo.label)
122123

124+
// shallowRef
125+
type Status = 'initial' | 'ready' | 'invalidating'
126+
const shallowStatus = shallowRef<Status>('initial')
127+
if (shallowStatus.value === 'initial') {
128+
expectType<Ref<Status>>(shallowStatus)
129+
expectType<Status>(shallowStatus.value)
130+
shallowStatus.value = 'invalidating'
131+
}
132+
133+
const refStatus = ref<Status>('initial')
134+
if (refStatus.value === 'initial') {
135+
expectType<Ref<Status>>(shallowStatus)
136+
expectType<Status>(shallowStatus.value)
137+
refStatus.value = 'invalidating'
138+
}
139+
123140
// proxyRefs: should return `reactive` directly
124141
const r1 = reactive({
125142
k: 'v'

0 commit comments

Comments
 (0)