Skip to content

Commit

Permalink
fix(types): fix provide type checking for ref value
Browse files Browse the repository at this point in the history
fix #8201
  • Loading branch information
yyx990803 committed May 1, 2023
1 parent be38922 commit de87e6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 14 additions & 1 deletion packages/dts-test/inject.test-d.ts
@@ -1,6 +1,9 @@
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)
Expand All @@ -14,3 +17,13 @@ expectType<number>(inject(key, () => 1, true /* treatDefaultAsFactory */))
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({}))
5 changes: 4 additions & 1 deletion packages/runtime-core/src/apiInject.ts
Expand Up @@ -6,7 +6,10 @@ import { warn } from './warning'

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().`)
Expand Down

0 comments on commit de87e6e

Please sign in to comment.