Skip to content

Commit 0eb771c

Browse files
authored
Fix(types): inject() return type (#295)
use undefined as return type instead of void close #290
1 parent 7c8386e commit 0eb771c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/apis/inject.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export function provide<T>(key: InjectionKey<T> | string, value: T): void {
3232
vm._provided[key as string] = value;
3333
}
3434

35-
export function inject<T>(key: InjectionKey<T> | string): T | void;
35+
export function inject<T>(key: InjectionKey<T> | string): T | undefined;
3636
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T;
37-
export function inject<T>(key: InjectionKey<T> | string, defaultValue?: T): T | void {
37+
export function inject<T>(key: InjectionKey<T> | string, defaultValue?: T): T | undefined {
3838
if (!key) {
3939
return defaultValue;
4040
}
@@ -43,9 +43,10 @@ export function inject<T>(key: InjectionKey<T> | string, defaultValue?: T): T |
4343
const val = resolveInject(key as InjectionKey<T>, vm);
4444
if (val !== NOT_FOUND) {
4545
return val;
46-
} else if (defaultValue !== undefined) {
46+
} else {
47+
if (defaultValue === undefined && process.env.NODE_ENV !== 'production') {
48+
warn(`Injection "${String(key)}" not found`, vm);
49+
}
4750
return defaultValue;
48-
} else if (process.env.NODE_ENV !== 'production') {
49-
warn(`Injection "${String(key)}" not found`, vm);
5051
}
5152
}

0 commit comments

Comments
 (0)