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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): toRefs should not do any ref unwrapping #4966

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
}

export type ToRefs<T = any> = {
// #2687: somehow using ToRef<T[K]> here turns the resulting type into
// a union of multiple Ref<*> types instead of a single Ref<* | *> type.
[K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>
[K in keyof T]: ToRef<T[K]>
}
Copy link
Contributor Author

@JensDll JensDll Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this was added in e315d84 the type for ToRef looked different, which is why doing ToRef only is fine again.

Related: Distributive Conditional Types

export function toRefs<T extends object>(object: T): ToRefs<T> {
if (__DEV__ && !isProxy(object)) {
Expand Down
72 changes: 37 additions & 35 deletions test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
toRef,
toRefs,
ToRefs,
shallowReactive,
watch
shallowReactive
} from './index'

function plainType(arg: number | Ref<number>) {
Expand Down Expand Up @@ -185,28 +184,45 @@ const p2 = proxyRefs(r2)
expectType<number>(p2.a)
expectType<Ref<string>>(p2.obj.k)

// toRef
const obj = {
a: 1,
b: ref(1)
}
expectType<Ref<number>>(toRef(obj, 'a'))
expectType<Ref<number>>(toRef(obj, 'b'))
// toRef and toRefs
{
const obj: {
a: number
b: Ref<number>
c: number | string
} = {
a: 1,
b: ref(1),
c: 1
}

const objWithUnionProp: { a: string | number } = {
a: 1
}
// toRef
expectType<Ref<number>>(toRef(obj, 'a'))
expectType<Ref<number>>(toRef(obj, 'b'))
// Should not distribute Refs over union
expectType<Ref<number | string>>(toRef(obj, 'c'))

// toRefs
expectType<{
a: Ref<number>
b: Ref<number>
// Should not distribute Refs over union
c: Ref<number | string>
}>(toRefs(obj))

// Both should not do any unwrapping
const someReactive = shallowReactive({
a: {
b: ref(42)
}
})

watch(toRef(objWithUnionProp, 'a'), value => {
expectType<string | number>(value)
})
const toRefResult = toRef(someReactive, 'a')
const toRefsResult = toRefs(someReactive)

// toRefs
const objRefs = toRefs(obj)
expectType<{
a: Ref<number>
b: Ref<number>
}>(objRefs)
expectType<Ref<number>>(toRefResult.value.b)
expectType<Ref<number>>(toRefsResult.a.value.b)
}

// #2687
interface AppData {
Expand Down Expand Up @@ -238,20 +254,6 @@ function testUnrefGenerics<T>(p: T | Ref<T>) {

testUnrefGenerics(1)

// #4732
describe('ref in shallow reactive', () => {
const baz = shallowReactive({
foo: {
bar: ref(42)
}
})

const foo = toRef(baz, 'foo')

expectType<Ref<number>>(foo.value.bar)
expectType<number>(foo.value.bar.value)
})

// #4771
describe('shallow reactive in reactive', () => {
const baz = reactive({
Expand Down