]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(reactivity): improve typings for `shallowRef` (#1780)
authorCarlos Rodrigues <david-181@hotmail.com>
Wed, 19 Aug 2020 03:34:29 +0000 (04:34 +0100)
committerGitHub <noreply@github.com>
Wed, 19 Aug 2020 03:34:29 +0000 (23:34 -0400)
packages/reactivity/src/ref.ts
test-dts/ref.test-d.ts

index 396e34e47d5f11cb95bede5892cf387ff3a32c4e..83bef572231992c3624976bf6102c28d47f410be 100644 (file)
@@ -35,7 +35,10 @@ export function ref(value?: unknown) {
   return createRef(value)
 }
 
-export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
+export function shallowRef<T extends object>(
+  value: T
+): T extends Ref ? T : Ref<T>
+export function shallowRef<T>(value: T): Ref<T>
 export function shallowRef<T = any>(): Ref<T | undefined>
 export function shallowRef(value?: unknown) {
   return createRef(value, true)
index 6430b15c57a8a99e1b90f0297267a7a431564e38..c961f4be37d1170631ac7a2553a61a8732dc8ef4 100644 (file)
@@ -1,6 +1,7 @@
 import {
   Ref,
   ref,
+  shallowRef,
   isRef,
   unref,
   reactive,
@@ -120,6 +121,22 @@ const state = reactive({
 
 expectType<string>(state.foo.label)
 
+// shallowRef
+type Status = 'initial' | 'ready' | 'invalidating'
+const shallowStatus = shallowRef<Status>('initial')
+if (shallowStatus.value === 'initial') {
+  expectType<Ref<Status>>(shallowStatus)
+  expectType<Status>(shallowStatus.value)
+  shallowStatus.value = 'invalidating'
+}
+
+const refStatus = ref<Status>('initial')
+if (refStatus.value === 'initial') {
+  expectType<Ref<Status>>(shallowStatus)
+  expectType<Status>(shallowStatus.value)
+  refStatus.value = 'invalidating'
+}
+
 // proxyRefs: should return `reactive` directly
 const r1 = reactive({
   k: 'v'