mirror of https://github.com/vuejs/core.git
fix(types): retain compatibility for provide() usage with explicit type parameter
This commit is contained in:
parent
d40d4a3380
commit
038cd830d5
|
@ -1,6 +1,7 @@
|
|||
import { provide, inject, ref, Ref, InjectionKey } from 'vue'
|
||||
import { expectType } from './utils'
|
||||
|
||||
// non-symbol keys
|
||||
provide('foo', 123)
|
||||
provide(123, 123)
|
||||
|
||||
|
@ -9,6 +10,8 @@ const key: InjectionKey<number> = Symbol()
|
|||
provide(key, 1)
|
||||
// @ts-expect-error
|
||||
provide(key, 'foo')
|
||||
// @ts-expect-error
|
||||
provide(key, null)
|
||||
|
||||
expectType<number | undefined>(inject(key))
|
||||
expectType<number>(inject(key, 1))
|
||||
|
@ -27,3 +30,13 @@ const injectionKeyRef = Symbol('key') as InjectionKey<Ref<Cube>>
|
|||
|
||||
// @ts-expect-error
|
||||
provide(injectionKeyRef, ref({}))
|
||||
|
||||
// naive-ui: explicit provide type parameter
|
||||
provide<Cube>('cube', { size: 123 })
|
||||
provide<Cube>(123, { size: 123 })
|
||||
provide<Cube>(injectionKeyRef, { size: 123 })
|
||||
|
||||
// @ts-expect-error
|
||||
provide<Cube>('cube', { size: 'foo' })
|
||||
// @ts-expect-error
|
||||
provide<Cube>(123, { size: 'foo' })
|
||||
|
|
|
@ -6,9 +6,9 @@ import { warn } from './warning'
|
|||
|
||||
export interface InjectionKey<T> extends Symbol {}
|
||||
|
||||
export function provide<T extends InjectionKey<any>>(
|
||||
key: T | string | number,
|
||||
value: T extends InjectionKey<infer V> ? V : any
|
||||
export function provide<T, K = InjectionKey<T> | string | number>(
|
||||
key: K,
|
||||
value: K extends InjectionKey<infer V> ? V : T
|
||||
) {
|
||||
if (!currentInstance) {
|
||||
if (__DEV__) {
|
||||
|
|
Loading…
Reference in New Issue