Skip to content
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
2 changes: 1 addition & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Ref<T = any> {
_shallow?: boolean
}

export type ToRef<T> = T extends Ref ? T : Ref<UnwrapRef<T>>
export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<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.
Expand Down
11 changes: 10 additions & 1 deletion test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
proxyRefs,
toRef,
toRefs,
ToRefs
ToRefs,
watch
} from './index'

function plainType(arg: number | Ref<number>) {
Expand Down Expand Up @@ -165,6 +166,14 @@ const obj = {
expectType<Ref<number>>(toRef(obj, 'a'))
expectType<Ref<number>>(toRef(obj, 'b'))

const objWithUnionProp: { a: string | number } = {
a: 1
}

watch(toRef(objWithUnionProp, 'a'), value => {
expectType<string | number>(value)
})

// toRefs
const objRefs = toRefs(obj)
expectType<{
Expand Down