Skip to content

Commit 6c3d592

Browse files
committed
feat: 为多个文件添加类型定义以增强类型安全性
1 parent b0d08d1 commit 6c3d592

File tree

6 files changed

+158
-57
lines changed

6 files changed

+158
-57
lines changed

packages/utils/src/date/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const maxDateValues = {
5454
const timezone1 = '-12:00,-11:00,-10:00,-09:30,-08:00,-07:00,-06:00,-05:00,-04:30,-04:00,-03:30,-02:00,-01:00'
5555
const timezone2 = '-00:00,+00:00,+01:00,+02:00,+03:00,+03:30,+04:00,+04:30,+05:00,+05:30,+05:45,+06:00'
5656
const timezone3 = '+06:30,+07:00,+08:00,+09:00,+10:00,+10:30,+11:00,+11:30,+12:00,+12:45,+13:00,+14:00'
57-
const timezones = [].concat(timezone1.split(','), timezone2.split(','), timezone3.split(','))
57+
const timezones: string[] = [...timezone1.split(','), ...timezone2.split(','), ...timezone3.split(',')]
5858

5959
const getTimezone = (date) => {
6060
const timezoneoffset = 0 - date.getTimezoneOffset() / 60
@@ -185,13 +185,17 @@ const iso8601DateParser = (m) => {
185185
}
186186
}
187187

188-
const dateParsers = [
188+
// 添加适当的类型定义
189+
type DateParser = (m: any) => Date | undefined
190+
type DateParserTuple = [RegExp, DateParser]
191+
192+
const dateParsers: DateParserTuple[] = [
189193
[yyyymmddReg, yyyymmddDateParser],
190194
[mmddyyyyReg, mmddyyyyDateParser],
191195
[iso8601Reg, iso8601DateParser]
192196
]
193197

194-
const parseDate = (str) => {
198+
const parseDate = (str: string) => {
195199
for (let i = 0, len = dateParsers.length; i < len; i++) {
196200
const m = dateParsers[i][0].exec(str)
197201

@@ -279,7 +283,7 @@ const getDateArray = (str, dateFormat) => {
279283
return arr
280284
}
281285

282-
const invalideTime = (time, min, max) => isNaN(time) || time < min || time > max
286+
const invalideTime = (time: number, min: number, max: number): boolean => isNaN(time) || time < min || time > max
283287

284288
const invalideValue = ({ year, month, date, hours, minutes, seconds, milliseconds }) =>
285289
invalideTime(year, 0, maxDateValues.YEAR) ||

packages/utils/src/fullscreen/screenfull.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313
import { on, off } from '../dom'
1414
import { isServer } from '../globalConfig'
1515

16+
// 添加接口定义
17+
interface FullscreenEvents {
18+
fullscreenElement: string
19+
fullscreenEnabled: string
20+
requestFullscreen: string
21+
exitFullscreen: string
22+
fullscreenchange: string
23+
fullscreenerror: string
24+
[key: string]: string
25+
}
26+
27+
interface Screenfull {
28+
request: (element?: any, options?: any) => Promise<void>
29+
exit: () => Promise<void>
30+
toggle: (element?: any, options?: any) => Promise<void>
31+
onchange: (callback: any) => void
32+
onerror: (callback: any) => void
33+
on: (event: string, callback: any) => void
34+
off: (event: string, callback: any) => void
35+
raw: FullscreenEvents | {}
36+
isFullscreen?: boolean
37+
element?: any
38+
isEnabled?: boolean
39+
}
40+
1641
const fullscreenApi = [
1742
'fullscreenElement',
1843
'fullscreenEnabled',
@@ -53,7 +78,7 @@ const fullscreenApiMap = [fullscreenApi, fullscreenApiWebkit, fullscreenApiMoz,
5378

5479
const document = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? window.document : {}
5580

56-
let fullscreenEvents = null
81+
let fullscreenEvents: FullscreenEvents | null = null
5782

5883
const getFullScreenEvents = () => {
5984
if (isServer) return
@@ -75,12 +100,12 @@ const getFullScreenEvents = () => {
75100

76101
getFullScreenEvents()
77102

78-
const eventNameMap = {
103+
const eventNameMap: { [key: string]: string | undefined } = {
79104
change: fullscreenEvents && fullscreenEvents.fullscreenchange,
80105
error: fullscreenEvents && fullscreenEvents.fullscreenerror
81106
}
82107

83-
const screenfull = {
108+
const screenfull: Screenfull = {
84109
request(element, options) {
85110
return new Promise((resolve, reject) => {
86111
const onFullscreenEntered = () => {

packages/utils/src/string/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,14 @@ const getResult = ({ type, res, formatText, string, reg, args }) => {
413413
return res
414414
}
415415

416-
const judgForFunc = (args, formatTypes, type) => {
416+
interface FormatTypesInterface {
417+
text: string
418+
url: string
419+
html: string
420+
[key: string]: string
421+
}
422+
423+
const judgForFunc = (args: any[], formatTypes: FormatTypesInterface, type: string): { args: any[]; type: string } => {
417424
const lastArg = args[args.length - 1]
418425

419426
if (lastArg !== formatTypes.text && lastArg !== formatTypes.url && lastArg !== formatTypes.html) {

0 commit comments

Comments
 (0)