Skip to content

Commit

Permalink
fix(toRef): ref created from union typed prop can't be used in watch (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni authored Feb 3, 2021
1 parent 6d5b623 commit 4ca4666
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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

0 comments on commit 4ca4666

Please sign in to comment.