Skip to content

Commit c64907d

Browse files
authored
fix(types): calling readonly() with ref() should return Readonly<Ref<T>> (#5212)
1 parent 171f5e9 commit c64907d

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

packages/reactivity/__tests__/readonly.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ describe('reactivity/readonly', () => {
438438
})
439439

440440
test('should make ref readonly', () => {
441-
const n: any = readonly(ref(1))
441+
const n = readonly(ref(1))
442+
// @ts-expect-error
442443
n.value = 2
443444
expect(n.value).toBe(1)
444445
expect(

packages/reactivity/src/reactive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export type DeepReadonly<T> = T extends Builtin
141141
: T extends Promise<infer U>
142142
? Promise<DeepReadonly<U>>
143143
: T extends Ref<infer U>
144-
? Ref<DeepReadonly<U>>
144+
? Readonly<Ref<DeepReadonly<U>>>
145145
: T extends {}
146146
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
147147
: Readonly<T>

test-dts/ref.test-d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
toRef,
1111
toRefs,
1212
ToRefs,
13-
shallowReactive
13+
shallowReactive,
14+
readonly
1415
} from './index'
1516

1617
function plainType(arg: number | Ref<number>) {
@@ -236,6 +237,9 @@ expectType<Ref<string>>(p2.obj.k)
236237
expectType<Ref<number>>(x)
237238
}
238239

240+
// readonly() + ref()
241+
expectType<Readonly<Ref<number>>>(readonly(ref(1)))
242+
239243
// #2687
240244
interface AppData {
241245
state: 'state1' | 'state2' | 'state3'

0 commit comments

Comments
 (0)