]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: fix + test inject API typing
authorEvan You <yyx990803@gmail.com>
Tue, 15 Sep 2020 01:26:28 +0000 (21:26 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 15 Sep 2020 01:26:28 +0000 (21:26 -0400)
ref: #2052

packages/runtime-core/src/apiInject.ts
test-dts/inject.test-d.ts [new file with mode: 0644]

index 02dc69e9d298d8e2fabce896131d233ca130a4cb..c12b6490849e7a262eae74afc3cb88dcc6ef2533 100644 (file)
@@ -31,7 +31,12 @@ export function inject<T>(key: InjectionKey<T> | string): T | undefined
 export function inject<T>(
   key: InjectionKey<T> | string,
   defaultValue: T,
-  treatDefaultAsFactory?: boolean
+  treatDefaultAsFactory?: false
+): T
+export function inject<T>(
+  key: InjectionKey<T> | string,
+  defaultValue: T | (() => T),
+  treatDefaultAsFactory: true
 ): T
 export function inject(
   key: InjectionKey<any> | string,
diff --git a/test-dts/inject.test-d.ts b/test-dts/inject.test-d.ts
new file mode 100644 (file)
index 0000000..69e06f1
--- /dev/null
@@ -0,0 +1,15 @@
+import { provide, inject, InjectionKey, expectType } from './index'
+
+const key: InjectionKey<number> = Symbol()
+
+provide(key, 1)
+// @ts-expect-error
+provide(key, 'foo')
+
+expectType<number | undefined>(inject(key))
+expectType<number>(inject(key, 1))
+expectType<number>(inject(key, () => 1, true /* treatDefaultAsFactory */))
+
+expectType<() => number>(inject('foo', () => 1))
+expectType<() => number>(inject('foo', () => 1, false))
+expectType<number>(inject('foo', () => 1, true))