Skip to content

Commit fd10511

Browse files
committed
💫 workflow(3-3):
1 parent 5d6db72 commit fd10511

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

easy/48-Exclude.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Description:Exclude
44
*/
55

6-
type Exclude<T, K> = T extends K ? never : T;
6+
export type Exclude<T, K> = T extends K ? never : T;
77
type IType1 = {
88
name: string;
99
age: number;

medium/02-GetReturnType.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 {};

medium/03-Omit.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 {};

medium/08-Readonly2.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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;

medium/09-DeepReadonly.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;

0 commit comments

Comments
 (0)