Skip to content

Commit b796331

Browse files
committed
feat(reactivity): enhance type inference for context-sensitive inputs
1 parent a989345 commit b796331

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/dts-test/reactivity.test-d.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { ref, readonly, shallowReadonly, Ref, reactive, markRaw } from 'vue'
1+
import {
2+
ref,
3+
readonly,
4+
shallowReadonly,
5+
Ref,
6+
reactive,
7+
markRaw,
8+
ComputedRef,
9+
computed
10+
} from 'vue'
211
import { describe, expectType } from './utils'
312

413
describe('should support DeepReadonly', () => {
@@ -62,3 +71,23 @@ describe('should unwrap tuple correctly', () => {
6271
const reactiveTuple = reactive(tuple)
6372
expectType<Ref<number>>(reactiveTuple[0])
6473
})
74+
75+
// #1930
76+
describe('should unwrap the computed type', () => {
77+
interface Bar {
78+
a: number
79+
b: (_: any) => void
80+
}
81+
82+
const computedA: ComputedRef<number> = computed(() => 1)
83+
84+
expect<{ a: number }>(reactive({ a: computedA }))
85+
86+
expect<Bar>(
87+
reactive({
88+
a: computedA, // issue happens here, but is should be okay
89+
// @ts-ignore
90+
b: _ => {} // error depending on --noImplicitAny
91+
})
92+
)
93+
})

packages/reactivity/src/reactive.ts

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ function getTargetType(value: Target) {
6464
// only unwrap nested ref
6565
export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
6666

67+
type NoInfer<T> = [T][T extends any ? 0 : never]
68+
6769
/**
6870
* Returns a reactive proxy of the object.
6971
*
@@ -80,6 +82,9 @@ export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
8082
* @see {@link https://vuejs.org/api/reactivity-core.html#reactive}
8183
*/
8284
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
85+
export function reactive<T extends object>(
86+
target: T
87+
): NoInfer<UnwrapNestedRefs<T>>
8388
export function reactive(target: object) {
8489
// if trying to observe a readonly proxy, return the readonly version.
8590
if (isReadonly(target)) {

0 commit comments

Comments
 (0)