Skip to content

Commit 7fe8d7b

Browse files
authored
fix: ts 错误: 类型实例化过深,且可能无限
1 parent aace726 commit 7fe8d7b

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

packages/@core/base/typings/src/helper.d.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
import type { ComputedRef, MaybeRef } from 'vue';
22

3+
/**
4+
* 类型级递归中增加深度计数
5+
*/
6+
type Increment<A extends unknown[]> = [...A, unknown];
37
/**
48
* 深层递归所有属性为可选
59
*/
6-
type DeepPartial<T> = T extends object
7-
? {
8-
[P in keyof T]?: DeepPartial<T[P]>;
9-
}
10-
: T;
10+
type DeepPartial<
11+
T,
12+
D extends number = 10,
13+
C extends unknown[] = [],
14+
> = C['length'] extends D
15+
? T
16+
: T extends object
17+
? {
18+
[P in keyof T]?: DeepPartial<T[P], D, Increment<C>>;
19+
}
20+
: T;
1121

1222
/**
1323
* 深层递归所有属性为只读
1424
*/
15-
type DeepReadonly<T> = {
16-
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
17-
};
25+
type DeepReadonly<
26+
T,
27+
D extends number = 10,
28+
C extends unknown[] = [],
29+
> = C['length'] extends D
30+
? T
31+
: T extends object
32+
? {
33+
readonly [P in keyof T]: DeepReadonly<T[P], D, Increment<C>>;
34+
}
35+
: T;
1836

1937
/**
2038
* 任意类型的异步函数

0 commit comments

Comments
 (0)