diff --git a/packages/dts-test/inject.test-d.ts b/packages/dts-test/inject.test-d.ts index 2e8341ff0ae..ace1700bb28 100644 --- a/packages/dts-test/inject.test-d.ts +++ b/packages/dts-test/inject.test-d.ts @@ -12,7 +12,7 @@ import { expectType } from './utils' provide('foo', 123) provide(123, 123) -const key: InjectionKey = Symbol() +const key = Symbol() as InjectionKey provide(key, 1) // @ts-expect-error diff --git a/packages/runtime-core/__tests__/apiInject.spec.ts b/packages/runtime-core/__tests__/apiInject.spec.ts index e5c9267e5bb..eb9134fe4b9 100644 --- a/packages/runtime-core/__tests__/apiInject.spec.ts +++ b/packages/runtime-core/__tests__/apiInject.spec.ts @@ -40,7 +40,7 @@ describe('api: provide/inject', () => { it('symbol keys', () => { // also verifies InjectionKey type sync - const key: InjectionKey = Symbol() + const key = Symbol() as InjectionKey const Provider = { setup() { diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index 44bb0390c99..6dc8d64a76b 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -55,7 +55,7 @@ export interface App { ): ComponentPublicInstance unmount(): void onUnmount(cb: () => void): void - provide | string | number>( + provide | string | symbol | number>( key: K, value: K extends InjectionKey ? V : T, ): this diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index f15983604bb..f824cf8e877 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -4,9 +4,10 @@ import { currentRenderingInstance } from './componentRenderContext' import { currentApp } from './apiCreateApp' import { warn } from './warning' -export interface InjectionKey extends Symbol {} +declare const InjectionKeySymbol: unique symbol +export type InjectionKey = symbol & { [InjectionKeySymbol]: T } -export function provide | string | number>( +export function provide | string | number | symbol>( key: K, value: K extends InjectionKey ? V : T, ) { @@ -31,19 +32,19 @@ export function provide | string | number>( } } -export function inject(key: InjectionKey | string): T | undefined +export function inject(key: InjectionKey | string | symbol): T | undefined export function inject( - key: InjectionKey | string, + key: InjectionKey | string | symbol, defaultValue: T, treatDefaultAsFactory?: false, ): T export function inject( - key: InjectionKey | string, + key: InjectionKey | string | symbol, defaultValue: T | (() => T), treatDefaultAsFactory: true, ): T export function inject( - key: InjectionKey | string, + key: InjectionKey | string | symbol, defaultValue?: unknown, treatDefaultAsFactory = false, ) {