fix(types): fix ToRefs type on union value types

fix #2687
This commit is contained in:
Evan You 2020-12-02 14:58:03 -05:00
parent 24a7a2db6c
commit e315d84936
2 changed files with 30 additions and 3 deletions

View File

@ -21,7 +21,11 @@ export interface Ref<T = any> {
} }
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> = { [K in keyof T]: ToRef<T[K]> } 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]>>
}
const convert = <T extends unknown>(val: T): T => const convert = <T extends unknown>(val: T): T =>
isObject(val) ? reactive(val) : val isObject(val) ? reactive(val) : val

View File

@ -8,7 +8,8 @@ import {
expectType, expectType,
proxyRefs, proxyRefs,
toRef, toRef,
toRefs toRefs,
ToRefs
} from './index' } from './index'
function plainType(arg: number | Ref<number>) { function plainType(arg: number | Ref<number>) {
@ -28,7 +29,6 @@ function plainType(arg: number | Ref<number>) {
const nestedRef = ref({ const nestedRef = ref({
foo: ref(1) foo: ref(1)
}) })
expectType<Ref<{ foo: number }>>(nestedRef)
expectType<{ foo: number }>(nestedRef.value) expectType<{ foo: number }>(nestedRef.value)
// ref boolean // ref boolean
@ -171,3 +171,26 @@ expectType<{
a: Ref<number> a: Ref<number>
b: Ref<number> b: Ref<number>
}>(objRefs) }>(objRefs)
// #2687
interface AppData {
state: 'state1' | 'state2' | 'state3'
}
const data: ToRefs<AppData> = toRefs(
reactive({
state: 'state1'
})
)
switch (data.state.value) {
case 'state1':
data.state.value = 'state2'
break
case 'state2':
data.state.value = 'state3'
break
case 'state3':
data.state.value = 'state1'
break
}