Skip to content

Commit 1d4d0d0

Browse files
meteorlxyJustineo
authored andcommitted
feat: provide better types for injection keys
1 parent 948d522 commit 1d4d0d0

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/ECharts.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
nextTick,
1515
PropType,
1616
watchEffect,
17-
Vue2
17+
Vue2,
18+
InjectionKey
1819
} from "vue-demi";
1920
import { init as initChart } from "echarts/core";
2021
import {
@@ -44,9 +45,9 @@ if (Vue2) {
4445
Vue2.config.ignoredElements.push(TAG_NAME);
4546
}
4647

47-
export const THEME_KEY = "ecTheme";
48-
export const INIT_OPTIONS_KEY = "ecInitOptions";
49-
export const UPDATE_OPTIONS_KEY = "ecUpdateOptions";
48+
export const THEME_KEY = "ecTheme" as unknown as InjectionKey<ThemeInjection>;
49+
export const INIT_OPTIONS_KEY = "ecInitOptions" as unknown as InjectionKey<InitOptionsInjection>;
50+
export const UPDATE_OPTIONS_KEY = "ecUpdateOptions" as unknown as InjectionKey<UpdateOptionsInjection>;
5051
export { LOADING_OPTIONS_KEY } from "./composables";
5152

5253
export default defineComponent({
@@ -69,15 +70,15 @@ export default defineComponent({
6970
const root = shallowRef<HTMLElement>();
7071
const chart = shallowRef<EChartsType>();
7172
const manualOption = shallowRef<Option>();
72-
const defaultTheme = inject(THEME_KEY, null) as ThemeInjection;
73+
const defaultTheme = inject(THEME_KEY, null);
7374
const defaultInitOptions = inject(
7475
INIT_OPTIONS_KEY,
7576
null
76-
) as InitOptionsInjection;
77+
);
7778
const defaultUpdateOptions = inject(
7879
UPDATE_OPTIONS_KEY,
7980
null
80-
) as UpdateOptionsInjection;
81+
);
8182

8283
const { autoresize, manualUpdate, loading, loadingOptions } = toRefs(props);
8384

src/composables/loading.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { inject, unref, computed, Ref, watchEffect } from "vue-demi";
1+
import { inject, unref, computed, Ref, watchEffect, InjectionKey } from "vue-demi";
22
import { EChartsType } from "../types";
33

4-
export const LOADING_OPTIONS_KEY = "ecLoadingOptions";
4+
export const LOADING_OPTIONS_KEY = "ecLoadingOptions" as unknown as InjectionKey<UnknownRecord | Ref<UnknownRecord>>;
55

66
type UnknownRecord = Record<string, unknown>;
77

@@ -10,9 +10,7 @@ export function useLoading(
1010
loading: Ref<boolean>,
1111
loadingOptions: Ref<UnknownRecord | undefined>
1212
): void {
13-
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {}) as
14-
| UnknownRecord
15-
| Ref<UnknownRecord>;
13+
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {})
1614
const realLoadingOptions = computed(() => ({
1715
...unref(defaultLoadingOptions),
1816
...loadingOptions?.value

0 commit comments

Comments
 (0)