From: Evan You Date: Tue, 15 Sep 2020 01:26:28 +0000 (-0400) Subject: types: fix + test inject API typing X-Git-Tag: v3.0.0-rc.11~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b2dc95378d0969032816d4fdce5cea0f5c5df3b1;p=thirdparty%2Fvuejs%2Fcore.git types: fix + test inject API typing ref: #2052 --- diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index 02dc69e9d2..c12b649084 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -31,7 +31,12 @@ export function inject(key: InjectionKey | string): T | undefined export function inject( key: InjectionKey | string, defaultValue: T, - treatDefaultAsFactory?: boolean + treatDefaultAsFactory?: false +): T +export function inject( + key: InjectionKey | string, + defaultValue: T | (() => T), + treatDefaultAsFactory: true ): T export function inject( key: InjectionKey | string, diff --git a/test-dts/inject.test-d.ts b/test-dts/inject.test-d.ts new file mode 100644 index 0000000000..69e06f170a --- /dev/null +++ b/test-dts/inject.test-d.ts @@ -0,0 +1,15 @@ +import { provide, inject, InjectionKey, expectType } from './index' + +const key: InjectionKey = Symbol() + +provide(key, 1) +// @ts-expect-error +provide(key, 'foo') + +expectType(inject(key)) +expectType(inject(key, 1)) +expectType(inject(key, () => 1, true /* treatDefaultAsFactory */)) + +expectType<() => number>(inject('foo', () => 1)) +expectType<() => number>(inject('foo', () => 1, false)) +expectType(inject('foo', () => 1, true))