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(toRef): ref created from union typed prop can't be used in watch #3048

Merged
merged 1 commit into from
Feb 3, 2021
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