Skip to content

Commit c10ff05

Browse files
committed
feat: add utility-types
1 parent 535a4c2 commit c10ff05

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

primitives/type-utils/src/fn.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export type Noop = () => void;
1212
* d: (x: number) => string;
1313
* e?: () => void; // optional also can be recognized
1414
* }
15-
* type FooFnTypes = FunctionUnion<Foo>; // (() => void) | ((x:number)=>string) | (()=>void)
15+
* type FooFnTypes = {
16+
* c: () => void;
17+
* d: (x: number) => string;
18+
* e?: (() => void) | undefined;
19+
}
1620
*/
1721
export type OnlyFunctions<T> = {
1822
[K in keyof T as NonNullable<T[K]> extends Fn ? K : never]: T[K];
@@ -26,7 +30,7 @@ export type OnlyFunctions<T> = {
2630
* b?: string;
2731
* c(): void;
2832
* d: (x: number) => string;
29-
* e?: () => void; // 可选也能识别
33+
* e?: () => void;
3034
* }
3135
* type FooFnKeys = FunctionKeys<Foo>; // 'c' | 'd' | 'e'
3236
*/
@@ -42,7 +46,7 @@ export type FunctionKeys<T> = {
4246
* b?: string;
4347
* c(): void;
4448
* d: (x: number) => string;
45-
* e?: () => void; // 可选也能识别
49+
* e?: () => void;
4650
* }
4751
* type FooFnTypes = FunctionUnion<Foo>; // (() => void) | ((x:number)=>string) | (()=>void)
4852
*/

primitives/type-utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export * from './fn';
22

33
export * from './form';
44

5+
export * from './utility-types';
6+
57
export * from './utils';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* DeepPartial<T>
3+
* @example
4+
* type User = {
5+
* name: string;
6+
* age: number;
7+
* address: {
8+
* city: string;
9+
* street: string;
10+
* };
11+
* };
12+
* type PartialUser = DeepPartial<User>;
13+
* // => { name?: string; age?: number; address?: { city?: string; street?: string } }
14+
*/
15+
export type DeepPartial<T> = {
16+
[K in keyof T]?: T[K] extends object ? (T[K] extends (...args: any[]) => any ? T[K] : DeepPartial<T[K]>) : T[K];
17+
};

0 commit comments

Comments
 (0)