fix(types): improve type of unref()

fix #3954
This commit is contained in:
Evan You 2021-07-01 15:20:49 -04:00
parent 69b74a806f
commit 127ed1b969
2 changed files with 8 additions and 1 deletions

View File

@ -85,7 +85,7 @@ export function triggerRef(ref: Ref) {
trigger(toRaw(ref), TriggerOpTypes.SET, 'value', __DEV__ ? ref.value : void 0) trigger(toRaw(ref), TriggerOpTypes.SET, 'value', __DEV__ ? ref.value : void 0)
} }
export function unref<T>(ref: T): T extends Ref<infer V> ? V : T { export function unref<T>(ref: T | Ref<T>): T {
return isRef(ref) ? (ref.value as any) : ref return isRef(ref) ? (ref.value as any) : ref
} }

View File

@ -203,3 +203,10 @@ switch (data.state.value) {
data.state.value = 'state1' data.state.value = 'state1'
break break
} }
// #3954
function testUnrefGenerics<T>(p: T | Ref<T>) {
expectType<T>(unref(p))
}
testUnrefGenerics(1)