]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): improve `app.provide` type checking (#10603)
authordopamine <coderzyou@gmail.com>
Mon, 10 Jun 2024 08:09:46 +0000 (16:09 +0800)
committerGitHub <noreply@github.com>
Mon, 10 Jun 2024 08:09:46 +0000 (16:09 +0800)
close #10602

packages/dts-test/inject.test-d.ts
packages/runtime-core/src/apiCreateApp.ts

index 55116f5692e24a3eea15d123f1a308d9975a3908..2e8341ff0aef524fd21068ad08fe07c12ac37226 100644 (file)
@@ -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<Cube>(injectionKeyRef, { size: 123 })
 provide<Cube>('cube', { size: 'foo' })
 // @ts-expect-error
 provide<Cube>(123, { size: 'foo' })
+
+// #10602
+const app = createApp({})
+// @ts-expect-error
+app.provide(injectionKeyRef, ref({}))
index 65c10166de7cbc8f600fca727c29aa86fbc36a0f..b7eb399f727179439dc1f636ae83178d6bf4dcb1 100644 (file)
@@ -50,7 +50,10 @@ export interface App<HostElement = any> {
     namespace?: boolean | ElementNamespace,
   ): ComponentPublicInstance
   unmount(): void
-  provide<T>(key: InjectionKey<T> | string, value: T): this
+  provide<T, K = InjectionKey<T> | string | number>(
+    key: K,
+    value: K extends InjectionKey<infer V> ? V : T,
+  ): this
 
   /**
    * Runs a function with the app as active instance. This allows using of `inject()` within the function to get access