Skip to content

Commit 0f7096a

Browse files
authored
feat: 将代码迁移至TS严格模式 (#684)
* feat: 将现有代码迁移至TS严格模式 - 禁止隐匿Any的This * feat: 将代码迁移至TS严格模式 - 启用JS严格模式 和 绑定类型检查 * feat: 移除无用钩子,修改为工具API * feat: 将代码迁移至TS严格模式 - 启用严格函数类型检查
1 parent 944f03a commit 0f7096a

File tree

15 files changed

+48
-84
lines changed

15 files changed

+48
-84
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dev:linux": "vite --mode development",
99
"build:test": "vite build --mode test",
1010
"build": "vue-tsc --noEmit && vite build --mode release",
11+
"build:type": "vue-tsc --noEmit",
1112
"build:site": "vue-tsc --noEmit && vite build --mode site",
1213
"preview": "vite preview",
1314
"lint": "eslint --ext .vue,.js,.jsx,.ts,.tsx ./ --max-warnings 0",

src/hooks/event/useWindowSizeFn.ts

-34
This file was deleted.

src/layouts/components/FrameContent.vue

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
</div>
77
</template>
88
<script lang="ts" setup>
9+
import { useWindowSize } from '@vueuse/core';
910
import debounce from 'lodash/debounce';
1011
import { computed, CSSProperties, ref, unref, watch } from 'vue';
1112
1213
import { prefix } from '@/config/global';
13-
import { useWindowSizeFn } from '@/hooks/event/useWindowSizeFn';
1414
import { useSettingStore } from '@/store';
1515
1616
defineProps({
1717
frameSrc: String,
1818
});
1919
20+
const { width, height } = useWindowSize();
21+
2022
const loading = ref(true);
2123
const heightRef = ref(window.innerHeight);
2224
const frameRef = ref<HTMLFrameElement>();
@@ -69,8 +71,8 @@ function hideLoading() {
6971
calcHeight();
7072
}
7173
72-
useWindowSizeFn(calcHeight, { immediate: true });
73-
74+
// 如果窗口大小发生变化
75+
watch([width, height], debounce(calcHeight, 250));
7476
watch(
7577
[() => settingStore.showFooter, () => settingStore.isUseTabsRouter, () => settingStore.showBreadcrumb],
7678
debounce(calcHeight, 250),

src/layouts/components/Header.vue

+10-6
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838
<translate-icon />
3939
</t-button>
4040
<t-dropdown-menu>
41-
<t-dropdown-item v-for="(lang, index) in langList" :key="index" :value="lang.value" @click="changeLang">{{
42-
lang.content
43-
}}</t-dropdown-item></t-dropdown-menu
41+
<t-dropdown-item
42+
v-for="(lang, index) in langList"
43+
:key="index"
44+
:value="lang.value"
45+
@click="(options) => changeLang(options.value as string)"
46+
>{{ lang.content }}</t-dropdown-item
47+
></t-dropdown-menu
4448
>
4549
</t-dropdown>
4650
<t-dropdown :min-column-width="120" trigger="click">
@@ -85,7 +89,7 @@ import { langList } from '@/locales/index';
8589
import { useLocale } from '@/locales/useLocale';
8690
import { getActive } from '@/router';
8791
import { useSettingStore, useUserStore } from '@/store';
88-
import type { MenuRoute } from '@/types/interface';
92+
import type { MenuRoute, ModeType } from '@/types/interface';
8993
9094
import MenuContent from './MenuContent.vue';
9195
import Notice from './Notice.vue';
@@ -147,11 +151,11 @@ const menuCls = computed(() => {
147151
},
148152
];
149153
});
150-
const menuTheme = computed(() => props.theme as 'light' | 'dark');
154+
const menuTheme = computed(() => props.theme as ModeType);
151155
152156
// 切换语言
153157
const { changeLocale } = useLocale();
154-
const changeLang = ({ value: lang }: { value: string }) => {
158+
const changeLang = (lang: string) => {
155159
changeLocale(lang);
156160
};
157161

src/layouts/components/LayoutContent.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:class="`${prefix}-layout-tabs-nav`"
88
:value="$route.path"
99
:style="{ position: 'sticky', top: 0, width: '100%' }"
10-
@change="handleChangeCurrentTab"
10+
@change="(value) => handleChangeCurrentTab(value as string)"
1111
@remove="handleRemove"
1212
@drag-sort="handleDragend"
1313
>

src/layouts/components/SideNav.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import AssetLogo from '@/assets/assets-t-logo.svg?component';
2626
import { prefix } from '@/config/global';
2727
import { getActive, getRoutesExpanded } from '@/router';
2828
import { useSettingStore } from '@/store';
29-
import type { MenuRoute } from '@/types/interface';
29+
import type { MenuRoute, ModeType } from '@/types/interface';
3030
3131
import pgk from '../../../package.json';
3232
import MenuContent from './MenuContent.vue';
@@ -55,7 +55,7 @@ const props = defineProps({
5555
default: '64px',
5656
},
5757
theme: {
58-
type: String as PropType<'light' | 'dark'>,
58+
type: String as PropType<ModeType>,
5959
default: 'light',
6060
},
6161
isCompact: {

src/pages/dashboard/base/components/MiddleChart.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
theme="primary"
1515
mode="date"
1616
:default-value="LAST_7_DAYS"
17-
@change="onCurrencyChange"
17+
@change="(value) => onCurrencyChange(value as string[])"
1818
/>
1919
</div>
2020
</template>

src/pages/dashboard/base/components/OutputOverview.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
theme="primary"
1515
mode="date"
1616
:default-value="LAST_7_DAYS"
17-
@change="onStokeDataChange"
17+
@change="(value) => onStokeDataChange(value as string[])"
1818
/>
1919
</template>
2020
<div id="stokeContainer" style="width: 100%; height: 351px" class="dashboard-chart-container"></div>

src/pages/dashboard/detail/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
theme="primary"
2727
mode="date"
2828
style="width: 248px"
29-
@change="onMaterialChange"
29+
@change="(value) => onMaterialChange(value as string[])"
3030
/>
3131
</template>
3232
<div id="lineContainer" style="width: 100%; height: 416px" />

src/pages/list/base/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
:header-affixed-top="headerAffixedTop"
3232
@page-change="rehandlePageChange"
3333
@change="rehandleChange"
34-
@select-change="rehandleSelectChange"
34+
@select-change="(value) => rehandleSelectChange(value as number[])"
3535
>
3636
<template #status="{ row }">
3737
<t-tag v-if="row.status === CONTRACT_STATUS.FAIL" theme="danger" variant="light">

src/pages/list/card/components/DialogForm.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const formVisible = ref(false);
7474
const formData = ref({ ...INITIAL_DATA });
7575
const textareaValue = ref('');
7676
77-
const onSubmit = ({ validateResult, firstError }: SubmitContext<FormData>) => {
77+
const onSubmit = ({ validateResult, firstError }: SubmitContext) => {
7878
if (!firstError) {
7979
MessagePlugin.success('提交成功');
8080
formVisible.value = false;

src/store/modules/setting.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import keys from 'lodash/keys';
22
import { defineStore } from 'pinia';
33
import { Color } from 'tvision-color';
44

5-
import { DARK_CHART_COLORS, LIGHT_CHART_COLORS } from '@/config/color';
5+
import { DARK_CHART_COLORS, LIGHT_CHART_COLORS, TColorSeries } from '@/config/color';
66
import STYLE_CONFIG from '@/config/style';
77
import { store } from '@/store';
8+
import { ModeType } from '@/types/interface';
89
import { generateColorMap, insertThemeStylesheet } from '@/utils/color';
910

10-
const state = {
11+
const state: Record<string, any> = {
1112
...STYLE_CONFIG,
1213
showSettingPanel: false,
13-
colorList: {},
14+
colorList: {} as TColorSeries,
1415
chartColors: LIGHT_CHART_COLORS,
1516
};
1617

@@ -23,22 +24,22 @@ export const useSettingStore = defineStore('setting', {
2324
showSidebar: (state) => state.layout !== 'top',
2425
showSidebarLogo: (state) => state.layout === 'side',
2526
showHeaderLogo: (state) => state.layout !== 'side',
26-
displayMode: (state): 'dark' | 'light' => {
27+
displayMode: (state): ModeType => {
2728
if (state.mode === 'auto') {
2829
const media = window.matchMedia('(prefers-color-scheme:dark)');
2930
if (media.matches) {
3031
return 'dark';
3132
}
3233
return 'light';
3334
}
34-
return state.mode as 'dark' | 'light';
35+
return state.mode as ModeType;
3536
},
36-
displaySideMode: (state): 'dark' | 'light' => {
37-
return state.sideMode as 'dark' | 'light';
37+
displaySideMode: (state): ModeType => {
38+
return state.sideMode as ModeType;
3839
},
3940
},
4041
actions: {
41-
async changeMode(mode: 'dark' | 'light' | 'auto') {
42+
async changeMode(mode: ModeType | 'auto') {
4243
let theme = mode;
4344

4445
if (mode === 'auto') {
@@ -50,7 +51,7 @@ export const useSettingStore = defineStore('setting', {
5051

5152
this.chartColors = isDarkMode ? DARK_CHART_COLORS : LIGHT_CHART_COLORS;
5253
},
53-
async changeSideMode(mode: 'dark' | 'light') {
54+
async changeSideMode(mode: ModeType) {
5455
const isDarkMode = mode === 'dark';
5556

5657
document.documentElement.setAttribute('side-mode', isDarkMode ? 'dark' : '');
@@ -75,23 +76,23 @@ export const useSettingStore = defineStore('setting', {
7576
step: 10,
7677
remainInput: false, // 是否保留输入 不保留会矫正不合适的主题色
7778
});
78-
colorMap = generateColorMap(brandTheme, newPalette, mode as 'light' | 'dark', brandColorIndex);
79+
colorMap = generateColorMap(brandTheme, newPalette, mode, brandColorIndex);
7980
this.colorList[colorKey] = colorMap;
8081
}
8182
// TODO 需要解决不停切换时有反复插入 style 的问题
82-
insertThemeStylesheet(brandTheme, colorMap, mode as 'light' | 'dark');
83+
insertThemeStylesheet(brandTheme, colorMap, mode);
8384
document.documentElement.setAttribute('theme-color', brandTheme);
8485
},
8586
updateConfig(payload: Partial<TState>) {
8687
for (const key in payload) {
8788
if (payload[key as TStateKey] !== undefined) {
88-
this[key] = payload[key as TStateKey];
89+
this[key as TStateKey] = payload[key as TStateKey];
8990
}
9091
if (key === 'mode') {
91-
this.changeMode(payload[key]);
92+
this.changeMode(payload[key] as ModeType);
9293
}
9394
if (key === 'sideMode') {
94-
this.changeSideMode(payload[key]);
95+
this.changeSideMode(payload[key] as ModeType);
9596
}
9697
if (key === 'brandTheme') {
9798
this.changeBrandTheme(payload[key]);

src/types/interface.d.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { TabValue } from 'tdesign-vue-next';
22
import { LocationQueryRaw, RouteRecordName } from 'vue-router';
33

4-
import STYLE_CONFIG from '@/config/style';
5-
64
export interface RouteMeta {
75
title?: string | Record<string, string>;
86
icon?: string;
@@ -32,14 +30,6 @@ export interface MenuRoute {
3230

3331
export type ModeType = 'dark' | 'light';
3432

35-
export type SettingType = typeof STYLE_CONFIG;
36-
37-
export type ClassName = { [className: string]: any } | ClassName[] | string;
38-
39-
export type CommonObjType = {
40-
[key: string]: string | number;
41-
};
42-
4333
export interface UserInfo {
4434
name: string;
4535
roles: string[];

src/utils/color.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import trim from 'lodash/trim';
33
import { Color } from 'tvision-color';
44

55
import { TColorToken } from '@/config/color';
6+
import { ModeType } from '@/types/interface';
67

78
/**
89
* 依据主题类型获取颜色
@@ -58,12 +59,7 @@ export function changeChartsTheme(chartsList: echarts.EChartsType[]): void {
5859
/**
5960
* 根据当前主题色、模式等情景 计算最后生成的色阶
6061
*/
61-
export function generateColorMap(
62-
theme: string,
63-
colorPalette: Array<string>,
64-
mode: 'light' | 'dark',
65-
brandColorIdx: number,
66-
) {
62+
export function generateColorMap(theme: string, colorPalette: Array<string>, mode: ModeType, brandColorIdx: number) {
6763
const isDarkMode = mode === 'dark';
6864

6965
if (isDarkMode) {
@@ -76,7 +72,7 @@ export function generateColorMap(
7672
colorPalette[0] = `${colorPalette[brandColorIdx]}20`;
7773
}
7874

79-
const colorMap = {
75+
const colorMap: TColorToken = {
8076
'--td-brand-color': colorPalette[brandColorIdx], // 主题色
8177
'--td-brand-color-1': colorPalette[0], // light
8278
'--td-brand-color-2': colorPalette[1], // focus
@@ -95,7 +91,7 @@ export function generateColorMap(
9591
/**
9692
* 将生成的样式嵌入头部
9793
*/
98-
export function insertThemeStylesheet(theme: string, colorMap: TColorToken, mode: 'light' | 'dark') {
94+
export function insertThemeStylesheet(theme: string, colorMap: TColorToken, mode: ModeType) {
9995
const isDarkMode = mode === 'dark';
10096
const root = !isDarkMode ? `:root[theme-color='${theme}']` : `:root[theme-color='${theme}'][theme-mode='dark']`;
10197

tsconfig.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
"paths": {
1717
"@/*": ["src/*"]
1818
},
19-
"noImplicitAny": true
19+
"noImplicitAny": true,
20+
"strictFunctionTypes": true,
21+
"strictBindCallApply": true,
22+
"noImplicitThis": true,
23+
"alwaysStrict": true,
2024
},
2125
"include": [
2226
"**/*.ts",

0 commit comments

Comments
 (0)