Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): calling readonly() with ref() should return Readonly<Ref<T>> #5212

Merged
merged 1 commit into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/reactivity/__tests__/readonly.spec.ts
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/reactive.ts
Expand Up @@ -139,7 +139,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>
Expand Down
6 changes: 5 additions & 1 deletion test-dts/ref.test-d.ts
Expand Up @@ -10,7 +10,8 @@ import {
toRef,
toRefs,
ToRefs,
shallowReactive
shallowReactive,
readonly
} from './index'

function plainType(arg: number | Ref<number>) {
Expand Down Expand Up @@ -231,6 +232,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'
Expand Down