-import { provide, inject, InjectionKey } from 'vue'
+import { provide, inject, ref, Ref, InjectionKey } from 'vue'
import { expectType } from './utils'
+provide('foo', 123)
+provide(123, 123)
+
const key: InjectionKey<number> = Symbol()
provide(key, 1)
expectType<() => number>(inject('foo', () => 1))
expectType<() => number>(inject('foo', () => 1, false))
expectType<number>(inject('foo', () => 1, true))
+
+// #8201
+type Cube = {
+ size: number
+}
+
+const injectionKeyRef = Symbol('key') as InjectionKey<Ref<Cube>>
+
+// @ts-expect-error
+provide(injectionKeyRef, ref({}))
export interface InjectionKey<T> extends Symbol {}
-export function provide<T>(key: InjectionKey<T> | string | number, value: T) {
+export function provide<T extends InjectionKey<any>>(
+ key: T | string | number,
+ value: T extends InjectionKey<infer V> ? V : any
+) {
if (!currentInstance) {
if (__DEV__) {
warn(`provide() can only be used inside setup().`)