Skip to content

Commit e34c845

Browse files
committed
feat: Improve type define
1 parent e741139 commit e34c845

File tree

10 files changed

+83
-74
lines changed

10 files changed

+83
-74
lines changed

packages/cli/src/constant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import path from 'node:path'
22

33
/** The path to the node_modules cache directory. */
4-
export const CACHE_DIR = path.resolve(process.cwd(), 'node_modules/.cache')
4+
export const CACHE_DIR: string = path.resolve(process.cwd(), 'node_modules/.cache')

packages/core/src/createPoll.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ interface PollStatus {
6464
maxTimes: number
6565
}
6666

67+
interface CreatePollReturn<T> {
68+
startPoll: () => void
69+
stopPoll: () => void
70+
getPollStatus: () => PollStatus
71+
}
72+
6773
/**
6874
* 创建一个轮询器
6975
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/createPoll.ts
@@ -86,7 +92,7 @@ interface PollStatus {
8692
* startPoll()
8793
* ```
8894
*/
89-
export function createPoll<T>(options: CreatePollOptions<T>) {
95+
export function createPoll<T>(options: CreatePollOptions<T>): CreatePollReturn<T> {
9096
if (isNegativeNumber(options.interval))
9197
throw new Error('interval must be a non-negative number')
9298
if (isNegativeNumber(options.maxTimes))

packages/core/src/doPathValue.ts

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// forked from https://github.com/g-makarov/dot-path-value
2-
export type Primitive = null | undefined | string | number | boolean | symbol | bigint
2+
export type Primitive = null | undefined | string | number | boolean | symbol | bigint;
33

4-
type ArrayKey = number
4+
type ArrayKey = number;
55

6-
type IsTuple<T extends readonly any[]> = number extends T['length'] ? false : true
6+
type IsTuple<T extends readonly any[]> = number extends T['length'] ? false : true;
77

8-
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>
8+
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>;
99

1010
export type PathConcat<TKey extends string | number, TValue> = TValue extends Primitive
1111
? `${TKey}`
12-
: `${TKey}` | `${TKey}.${Path<TValue>}`
12+
: `${TKey}` | `${TKey}.${Path<TValue>}`;
1313

1414
export type Path<T> = T extends readonly (infer V)[]
1515
? IsTuple<T> extends true
@@ -19,15 +19,15 @@ export type Path<T> = T extends readonly (infer V)[]
1919
: PathConcat<ArrayKey, V>
2020
: {
2121
[K in keyof T]-?: PathConcat<K & string, T[K]>;
22-
}[keyof T]
22+
}[keyof T];
2323

2424
type ArrayPathConcat<TKey extends string | number, TValue> = TValue extends Primitive
2525
? never
2626
: TValue extends readonly (infer U)[]
27-
? U extends Primitive
28-
? never
29-
: `${TKey}` | `${TKey}.${ArrayPath<TValue>}`
30-
: `${TKey}.${ArrayPath<TValue>}`
27+
? U extends Primitive
28+
? never
29+
: `${TKey}` | `${TKey}.${ArrayPath<TValue>}`
30+
: `${TKey}.${ArrayPath<TValue>}`;
3131

3232
export type ArrayPath<T> = T extends readonly (infer V)[]
3333
? IsTuple<T> extends true
@@ -37,7 +37,7 @@ export type ArrayPath<T> = T extends readonly (infer V)[]
3737
: ArrayPathConcat<ArrayKey, V>
3838
: {
3939
[K in keyof T]-?: ArrayPathConcat<K & string, T[K]>;
40-
}[keyof T]
40+
}[keyof T];
4141

4242
export type PathValue<T, TPath extends Path<T> | ArrayPath<T>> = T extends any
4343
? TPath extends `${infer K}.${infer R}`
@@ -48,66 +48,47 @@ export type PathValue<T, TPath extends Path<T> | ArrayPath<T>> = T extends any
4848
: PathValue<T[K], R>
4949
: never
5050
: K extends `${ArrayKey}`
51-
? T extends readonly (infer V)[]
52-
? PathValue<V, R & Path<V>>
53-
: never
51+
? T extends readonly (infer V)[]
52+
? PathValue<V, R & Path<V>>
5453
: never
54+
: never
5555
: TPath extends keyof T
56-
? T[TPath]
57-
: TPath extends `${ArrayKey}`
58-
? T extends readonly (infer V)[]
59-
? V
60-
: never
61-
: never
62-
: never
63-
/**
64-
* It takes an object and a path, and returns the value at that path, type safe.
65-
* @param {T} obj - The object to get the value from.
66-
* @param {TPath} path - The path to the value you want to get.
67-
* @returns The value of the path in the object.
68-
* @forked from https://github.com/g-makarov/dot-path-value
69-
* @example
70-
* ```ts
71-
* const obj = {
72-
* a: {
73-
* b: {
74-
* c: [
75-
* { d: 1 }
76-
* ]
77-
* },
78-
* }
79-
* getByPath(obj, 'a.b.c[0].d') // 1
80-
* getByPath(obj, 'a.b.c.0') // { d: 1 }
81-
* ```
82-
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/doPathValue.ts
83-
*/
56+
? T[TPath]
57+
: TPath extends `${ArrayKey}`
58+
? T extends readonly (infer V)[]
59+
? V
60+
: never
61+
: never
62+
: never;
63+
8464
export function getByPath<T extends Record<string, any>, TPath extends Path<T>>(
8565
obj: T,
8666
path: TPath,
8767
): PathValue<T, TPath> {
88-
return path.split('.').reduce((acc, key) => acc?.[key], obj) as PathValue<T, TPath>
68+
return path.split('.').reduce((acc, key) => acc?.[key], obj) as PathValue<T, TPath>;
8969
}
9070

9171
export function setByPath<T extends Record<string, any>, TPath extends Path<T>>(
9272
obj: T,
9373
path: TPath,
9474
value: PathValue<T, TPath>,
95-
) {
96-
const segments = path.split('.') as TPath[]
97-
const lastKey = segments.pop()
75+
): T {
76+
const segments = path.split('.') as TPath[];
77+
const lastKey = segments.pop();
9878

99-
let target: T = obj
79+
let target: T = obj;
10080

10181
for (let i = 0; i < segments.length; i++) {
102-
const key = segments[i] as TPath
103-
if (!(key in target))
104-
target[key] = {} as PathValue<T, TPath>
105-
106-
target = target[key]
82+
const key = segments[i] as TPath;
83+
if (!(key in target)) {
84+
target[key] = {} as PathValue<T, TPath>;
85+
}
86+
target = target[key];
10787
}
10888

109-
if (lastKey)
110-
target[lastKey] = value
89+
if (lastKey) {
90+
target[lastKey] = value;
91+
}
11192

112-
return obj
113-
}
93+
return obj;
94+
}

packages/core/src/stringDesensitize.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Level = 'low' | 'medium' | 'high'
1717
* ```
1818
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts
1919
*/
20-
export function desensitizeName(name?: string) {
20+
export function desensitizeName(name?: string): string | undefined {
2121
if (!isString(name))
2222
return name
2323

@@ -44,7 +44,7 @@ export function desensitizeName(name?: string) {
4444
* ```
4545
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts
4646
*/
47-
export function desensitizePhone(phone?: string) {
47+
export function desensitizePhone(phone?: string): string | undefined {
4848
if (!isString(phone))
4949
return phone
5050

@@ -72,7 +72,7 @@ export function desensitizePhone(phone?: string) {
7272
* ```
7373
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts
7474
*/
75-
export function desensitizeIDCard(idCard?: string, level: Level = 'low') {
75+
export function desensitizeIDCard(idCard?: string, level: Level = 'low'): string | undefined {
7676
if (!isString(idCard))
7777
return idCard
7878

@@ -97,7 +97,7 @@ export function desensitizeIDCard(idCard?: string, level: Level = 'low') {
9797
* ```
9898
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts
9999
*/
100-
export function desensitizeEmail(email?: string) {
100+
export function desensitizeEmail(email?: string): string | undefined {
101101
if (!isString(email))
102102
return email
103103

packages/core/src/stringFormatter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* ```
1111
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts
1212
*/
13-
export function formatterBankCard(str?: string) {
13+
export function formatterBankCard(str?: string): string {
1414
return `${str}`.replace(/\D/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
1515
}
1616

@@ -28,7 +28,7 @@ const PHONE_LENGTH = 11
2828
* ```
2929
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts
3030
*/
31-
export function formatterPhoneNumber(str?: string) {
31+
export function formatterPhoneNumber(str?: string): string {
3232
return `${str}`
3333
.replace(/\D/g, '')
3434
.substring(0, PHONE_LENGTH)
@@ -54,7 +54,7 @@ const ID_CARD_LENGTH = 18
5454
* ```
5555
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts
5656
*/
57-
export function formatterIdCard(str?: string) {
57+
export function formatterIdCard(str?: string): string {
5858
return `${str}`
5959
.replace(/[^0-9xX]/g, '')
6060
.substring(0, ID_CARD_LENGTH)

packages/dom/src/panzoom/core.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { PanZoomOptions, Transform } from './type'
22
import { applyTransform, getScaleMultiplier } from './utils'
33

4+
type PanZoomReturn = {
5+
destroy: () => void
6+
}
47
/**
58
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/panzoom/index.ts
69
*/
@@ -9,7 +12,7 @@ export function panzoom(target: HTMLElement, {
912
maxZoom = 5,
1013
minZoom = 0.2,
1114
onTransform,
12-
}: PanZoomOptions = {}) {
15+
}: PanZoomOptions = {}): PanZoomReturn {
1316
const parentNode = target.parentNode as HTMLElement
1417

1518
if (!parentNode)

packages/dom/src/setCssVar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { isPlainObject } from '@utopia-utils/share'
1515
* setCssVar({ '--color': 'red', '--size': '10px' }, document.body)
1616
* ```
1717
*/
18-
export function setCssVar(variables: Record<string, string | null>, root = window?.document?.documentElement): void {
18+
export function setCssVar(variables: Record<string, string | null>, root: HTMLElement = window?.document?.documentElement): void {
1919
if (variables && isPlainObject(variables) && root) {
2020
Object.keys(variables).forEach((key) => {
2121
root.style.setProperty(key, variables[key])

packages/share/src/is.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const isString = (val: unknown): val is string => typeof val === 'string'
55
export const isNumber = (val: unknown): val is number => typeof val === 'number'
66
export const isFunction = (val: unknown): val is Function => typeof val === 'function'
77
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
8-
export const isArray = Array.isArray
8+
export const isArray = (val: unknown): val is any[] => Array.isArray(val)
99
export const isRegExp = (val: unknown): val is RegExp => toTypeString(val) === 'RegExp'
1010
export const isMap = (val: unknown): val is Map<any, any> => toTypeString(val) === 'Map'
1111
export const isSet = (val: unknown): val is Set<any> => toTypeString(val) === 'Set'

packages/vueuse/src/useSmsCountdown/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { computed, ref } from 'vue'
2+
import type { Ref, ComputedRef } from 'vue'
23

34
import type { MaybeRef } from '../types'
45
import { toValue, tryOnScopeDispose } from '../utils'
56

6-
export interface UseSmsCountdownOptions {
7+
export type UseSmsCountdownOptions = {
78
/**
89
* 倒计时总共的时间(s)
910
* @default 60s
@@ -25,8 +26,15 @@ export interface UseSmsCountdownOptions {
2526
*/
2627
durationText?: string
2728
}
29+
type UseSmsCountdownReturn = {
30+
counts: Ref<number>
31+
canSend: ComputedRef<boolean>
32+
text: ComputedRef<string>
33+
startCountdown: () => void
34+
stopCountdown: () => void
35+
}
2836

29-
export function useSmsCountdown(options?: UseSmsCountdownOptions) {
37+
export function useSmsCountdown(options?: UseSmsCountdownOptions): UseSmsCountdownReturn {
3038
const { totalSecond = 60, sendAble = true, startText = '获取验证码', durationText = 'x秒后重发' } = options || {}
3139

3240
if (totalSecond <= 0 && totalSecond % 1 !== 0)

packages/vueuse/src/useTable/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Ref } from 'vue';
1+
import type { Ref, ShallowRef } from 'vue';
22

33
import { onScopeDispose, ref, shallowRef } from 'vue';
44

@@ -29,6 +29,17 @@ type TableSort = {
2929
field: string;
3030
order?: 'ascend' | 'descend' | null;
3131
};
32+
type UseTableReturn<Filters> = {
33+
pageSize: Ref<number>;
34+
currentPage: Ref<number>;
35+
sort: ShallowRef<TableSort | undefined>;
36+
filters: Ref<Filters>;
37+
search: {
38+
submit: () => void;
39+
reset: () => void;
40+
formState: Ref<Filters>;
41+
};
42+
};
3243

3344
/* 强制刷新标识符,避免污染用户数据 */
3445
const FORCE_REFRESH_SYMBOL = '__now__';
@@ -39,7 +50,7 @@ const FORCE_REFRESH_SYMBOL = '__now__';
3950
*/
4051
export function useTable<Filters extends Record<string, any>>(
4152
options?: UseTableOptions<Filters>,
42-
) {
53+
): UseTableReturn<Filters> {
4354
const {
4455
defaultFilters = {} as Filters,
4556
searchType = 'advance',
@@ -66,9 +77,9 @@ export function useTable<Filters extends Record<string, any>>(
6677
if (currentPage.value !== 1) currentPage.value = 1; // 更新 currentPage, 会触发 watch, 重新设置 query
6778
};
6879

69-
const submit = () => onFormChange({ forceRefresh: true });
80+
const submit: () => void = () => onFormChange({ forceRefresh: true });
7081

71-
const reset = () => {
82+
const reset: () => void = () => {
7283
formState.value = deepClone(getDefaultFilters(defaultFilters));
7384

7485
onFormChange({ forceRefresh: true });

0 commit comments

Comments
 (0)