]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): calling readonly() with ref() should return Readonly<Ref<T>> (#5212)
authorHcySunYang <HcySunYang@outlook.com>
Fri, 21 Jan 2022 07:33:30 +0000 (15:33 +0800)
committerGitHub <noreply@github.com>
Fri, 21 Jan 2022 07:33:30 +0000 (02:33 -0500)
packages/reactivity/__tests__/readonly.spec.ts
packages/reactivity/src/reactive.ts
test-dts/ref.test-d.ts

index 72a873a016ee94790c2bd0d304bddc734b388e4d..4462061b3a9b5dd882908cb88453e4f7f78dc9c4 100644 (file)
@@ -438,7 +438,8 @@ describe('reactivity/readonly', () => {
   })
 
   test('should make ref readonly', () => {
-    const n: any = readonly(ref(1))
+    const n = readonly(ref(1))
+    // @ts-expect-error
     n.value = 2
     expect(n.value).toBe(1)
     expect(
index 7d7e591fc4adba6440350d7c5c8ea61cff8f8387..f160b21110db53c24f5b888d22e863b6d8c24e8a 100644 (file)
@@ -141,7 +141,7 @@ export type DeepReadonly<T> = T extends Builtin
   : T extends Promise<infer U>
   ? Promise<DeepReadonly<U>>
   : T extends Ref<infer U>
-  ? Ref<DeepReadonly<U>>
+  ? Readonly<Ref<DeepReadonly<U>>>
   : T extends {}
   ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
   : Readonly<T>
index e585470d5e4c03e04ce03faf45734a23d3c3f93a..bc1deb05863f03c7ea304b59db946eba68856d54 100644 (file)
@@ -10,7 +10,8 @@ import {
   toRef,
   toRefs,
   ToRefs,
-  shallowReactive
+  shallowReactive,
+  readonly
 } from './index'
 
 function plainType(arg: number | Ref<number>) {
@@ -236,6 +237,9 @@ expectType<Ref<string>>(p2.obj.k)
   expectType<Ref<number>>(x)
 }
 
+// readonly() + ref()
+expectType<Readonly<Ref<number>>>(readonly(ref(1)))
+
 // #2687
 interface AppData {
   state: 'state1' | 'state2' | 'state3'