]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(types): add test for ref/shallowRef generic casting
authorEvan You <yyx990803@gmail.com>
Sat, 16 Dec 2023 05:46:11 +0000 (13:46 +0800)
committerEvan You <yyx990803@gmail.com>
Sat, 16 Dec 2023 05:46:11 +0000 (13:46 +0800)
packages/dts-test/ref.test-d.ts

index f66ff9269079472a688587dfd5422b7ae49dc68e..62bad77c2ee80c2f19562a049491c7d606e312f2 100644 (file)
@@ -163,6 +163,17 @@ const state = reactive({
 
 expectType<string>(state.foo.label)
 
+describe('ref with generic', <T extends { name: string }>() => {
+  const r = {} as T
+  const s = ref(r)
+  expectType<string>(s.value.name)
+
+  const rr = {} as MaybeRef<T>
+  // should at least allow casting
+  const ss = ref(rr) as Ref<T>
+  expectType<string>(ss.value.name)
+})
+
 // shallowRef
 type Status = 'initial' | 'ready' | 'invalidating'
 const shallowStatus = shallowRef<Status>('initial')
@@ -206,6 +217,11 @@ describe('shallowRef with generic', <T extends { name: string }>() => {
   const s = shallowRef(r)
   expectType<string>(s.value.name)
   expectType<ShallowRef<T>>(shallowRef(r))
+
+  const rr = {} as MaybeRef<T>
+  // should at least allow casting
+  const ss = shallowRef(rr) as Ref<T> | ShallowRef<T>
+  expectType<string>(ss.value.name)
 })
 
 {