File tree Expand file tree Collapse file tree 5 files changed +128
-1
lines changed Expand file tree Collapse file tree 5 files changed +128
-1
lines changed Original file line number Diff line number Diff line change 3
3
* @Description :Exclude
4
4
*/
5
5
6
- type Exclude < T , K > = T extends K ? never : T ;
6
+ export type Exclude < T , K > = T extends K ? never : T ;
7
7
type IType1 = {
8
8
name : string ;
9
9
age : number ;
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Date : 2022-03-03
3
+ * @Description : Get Retrun Type
4
+ */
5
+
6
+ const fn = ( v : boolean ) => {
7
+ if ( v ) return 1 ;
8
+ else return 2 ;
9
+ } ;
10
+
11
+ type MyReturnType < T extends ( ...args : any [ ] ) => any > = T extends (
12
+ ...args : any [ ]
13
+ ) => infer R
14
+ ? R
15
+ : any ;
16
+
17
+ type a = MyReturnType < typeof fn > ; // should be "1 | 2"
18
+
19
+ export { } ;
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Date : 2022-03-03
3
+ * @Description : Omit
4
+ */
5
+
6
+ interface Todo {
7
+ title : string ;
8
+ description : string ;
9
+ completed : boolean ;
10
+ }
11
+
12
+ type MyOmit < T , K extends string | number | symbol > = {
13
+ [ U in Exclude < keyof T , K > ] : false ;
14
+ } ;
15
+
16
+ type TodoPreview = MyOmit < Todo , "description" | "title" > ;
17
+
18
+ const todo : TodoPreview = {
19
+ completed : false ,
20
+ } ;
21
+
22
+ export { } ;
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Date : 2022-03-03
3
+ * @Description : Readonly2
4
+ */
5
+
6
+ interface Todo {
7
+ title : string ;
8
+ description : string ;
9
+ completed : boolean ;
10
+ }
11
+
12
+ type MyReadonly2 < T , K extends keyof T > = {
13
+ readonly [ P in K ] : T [ P ] ;
14
+ } & {
15
+ [ L in Exclude < keyof T , K > ] : T [ L ] ;
16
+ } ;
17
+
18
+ const todo : MyReadonly2 < Todo , "title" | "description" > = {
19
+ title : "Hey" ,
20
+ description : "foobar" ,
21
+ completed : false ,
22
+ } ;
23
+
24
+ //todo.title = "Hello"; // Error: cannot reassign a readonly property
25
+ //todo.description = "barFoo"; // Error: cannot reassign a readonly property
26
+ todo . completed = true ; // OK
27
+
28
+ export { } ;
29
+
30
+ type ReadOnly3 < T , K > = K extends keyof T
31
+ ? {
32
+ readonly [ U in K ] : T [ U ] ;
33
+ } & {
34
+ [ L in Exclude < keyof T , K > ] : T [ L ] ;
35
+ }
36
+ : T ;
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Date : 2022-03-03
3
+ * @Description : DeepReadonly
4
+ */
5
+
6
+ type X = {
7
+ x : {
8
+ a : 1 ;
9
+ b : "hi" ;
10
+ } ;
11
+ y : "hey" ;
12
+ } ;
13
+
14
+ //1
15
+
16
+ type DeepReadonly < T > = {
17
+ readonly [ P in keyof T ] : T [ P ] extends { [ index : string ] : unknown }
18
+ ? DeepReadonly < T [ P ] >
19
+ : T [ P ] ;
20
+ } ;
21
+
22
+ //2
23
+
24
+ type DeepReadonly2 < T > = keyof T extends never
25
+ ? T
26
+ : {
27
+ [ P in keyof T ] : DeepReadonly2 < T [ P ] > ;
28
+ } ;
29
+
30
+ type Expected = {
31
+ readonly x : {
32
+ readonly a : 1 ;
33
+ readonly b : "hi" ;
34
+ } ;
35
+ readonly y : "hey" ;
36
+ } ;
37
+
38
+ type Todo = DeepReadonly < X > ; // should be same as `Expected`
39
+
40
+ //判断是否为对象 keyof T extends never ? :
41
+ // extends {[index;string]:unknown}
42
+
43
+ const test : Todo = {
44
+ x : {
45
+ a : 1 ,
46
+ b : "hi" ,
47
+ } ,
48
+ y : "hey" ,
49
+ } ;
50
+ //test.x.a = 123;
You can’t perform that action at this time.
0 commit comments