Skip to content

Commit de87e6e

Browse files
committed
fix(types): fix provide type checking for ref value
fix #8201
1 parent be38922 commit de87e6e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { provide, inject, InjectionKey } from 'vue'
1+
import { provide, inject, ref, Ref, InjectionKey } from 'vue'
22
import { expectType } from './utils'
33

4+
provide('foo', 123)
5+
provide(123, 123)
6+
47
const key: InjectionKey<number> = Symbol()
58

69
provide(key, 1)
@@ -14,3 +17,13 @@ expectType<number>(inject(key, () => 1, true /* treatDefaultAsFactory */))
1417
expectType<() => number>(inject('foo', () => 1))
1518
expectType<() => number>(inject('foo', () => 1, false))
1619
expectType<number>(inject('foo', () => 1, true))
20+
21+
// #8201
22+
type Cube = {
23+
size: number
24+
}
25+
26+
const injectionKeyRef = Symbol('key') as InjectionKey<Ref<Cube>>
27+
28+
// @ts-expect-error
29+
provide(injectionKeyRef, ref({}))

packages/runtime-core/src/apiInject.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { warn } from './warning'
66

77
export interface InjectionKey<T> extends Symbol {}
88

9-
export function provide<T>(key: InjectionKey<T> | string | number, value: T) {
9+
export function provide<T extends InjectionKey<any>>(
10+
key: T | string | number,
11+
value: T extends InjectionKey<infer V> ? V : any
12+
) {
1013
if (!currentInstance) {
1114
if (__DEV__) {
1215
warn(`provide() can only be used inside setup().`)

0 commit comments

Comments
 (0)