From: dopamine Date: Mon, 10 Jun 2024 08:09:46 +0000 (+0800) Subject: fix(types): improve `app.provide` type checking (#10603) X-Git-Tag: v3.4.28~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=612bbf0507cbe39d701acc5dff11824802078063;p=thirdparty%2Fvuejs%2Fcore.git fix(types): improve `app.provide` type checking (#10603) close #10602 --- diff --git a/packages/dts-test/inject.test-d.ts b/packages/dts-test/inject.test-d.ts index 55116f5692..2e8341ff0a 100644 --- a/packages/dts-test/inject.test-d.ts +++ b/packages/dts-test/inject.test-d.ts @@ -1,4 +1,11 @@ -import { type InjectionKey, type Ref, inject, provide, ref } from 'vue' +import { + type InjectionKey, + type Ref, + createApp, + inject, + provide, + ref, +} from 'vue' import { expectType } from './utils' // non-symbol keys @@ -40,3 +47,8 @@ provide(injectionKeyRef, { size: 123 }) provide('cube', { size: 'foo' }) // @ts-expect-error provide(123, { size: 'foo' }) + +// #10602 +const app = createApp({}) +// @ts-expect-error +app.provide(injectionKeyRef, ref({})) diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index 65c10166de..b7eb399f72 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -50,7 +50,10 @@ export interface App { namespace?: boolean | ElementNamespace, ): ComponentPublicInstance unmount(): void - provide(key: InjectionKey | string, value: T): this + provide | string | number>( + key: K, + value: K extends InjectionKey ? V : T, + ): this /** * Runs a function with the app as active instance. This allows using of `inject()` within the function to get access