Skip to content

Commit b2dc953

Browse files
committed
types: fix + test inject API typing
ref: #2052
1 parent b29bc0d commit b2dc953

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/runtime-core/src/apiInject.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ export function inject<T>(key: InjectionKey<T> | string): T | undefined
3131
export function inject<T>(
3232
key: InjectionKey<T> | string,
3333
defaultValue: T,
34-
treatDefaultAsFactory?: boolean
34+
treatDefaultAsFactory?: false
35+
): T
36+
export function inject<T>(
37+
key: InjectionKey<T> | string,
38+
defaultValue: T | (() => T),
39+
treatDefaultAsFactory: true
3540
): T
3641
export function inject(
3742
key: InjectionKey<any> | string,

test-dts/inject.test-d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { provide, inject, InjectionKey, expectType } from './index'
2+
3+
const key: InjectionKey<number> = Symbol()
4+
5+
provide(key, 1)
6+
// @ts-expect-error
7+
provide(key, 'foo')
8+
9+
expectType<number | undefined>(inject(key))
10+
expectType<number>(inject(key, 1))
11+
expectType<number>(inject(key, () => 1, true /* treatDefaultAsFactory */))
12+
13+
expectType<() => number>(inject('foo', () => 1))
14+
expectType<() => number>(inject('foo', () => 1, false))
15+
expectType<number>(inject('foo', () => 1, true))

0 commit comments

Comments
 (0)