-
Notifications
You must be signed in to change notification settings - Fork 0
/
在线演示.ts
179 lines (151 loc) · 4.18 KB
/
在线演示.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function comp<Type>(a:Type, b:Type) {
if (a.length >= b.length) {
return a;
}
return b;
}
function comp1<Type extends {length:number}>(a:Type, b:Type) {
if (a.length >= b.length) {
return a;
}
return b;
}
comp1([1, 2], [1, 2, 3]) // 正确
comp1('ab', 'abc') // 正确
comp1(1, 2) // 报错
function deb<T extends any[],R>(fn:(...args:T)=>R,time:number):(...args:T)=>void {
return ()=>{}
}
function handle(a:number,b:string){ return [a,b] }
const f = deb(handle,200)
f(1,'1')
type MyObj = { foo: number, bar: string, age:{
a:string
}};
type Keys = keyof MyObj; // 'foo'|'bar'
let case1:Keys='foo';
type Case2 = {
[P in keyof MyObj]:string
}
type MyReadonly1<T> ={
+readonly[P in keyof T]:T[P]
}
type MyRequired<T> ={
[P in keyof T]-?:T[P]
}
type MyPartial<T> ={
[P in keyof T]+?:T[P]
}
type MydeepPartial<T> ={
[P in keyof T]+?:T[P] extends Object?MydeepPartial<T[P]>:T[P]
}
type case4 = MydeepPartial<MyObj>
let case5:case4={
age:{
}
}
type LiteralTypeName<T> =
T extends undefined ? "undefined" :
T extends null ? "null" :
T extends boolean ? "boolean" :
T extends number ? "number" :
T extends bigint ? "bigint" :
T extends string ? "string" :
never;
type Result2 = LiteralTypeName<true | 1 | 'a'>;
// "boolean"|'number'|'string'
type myexclude<T,U>=T extends U?never:T
type myextract<T,U>=U extends T?T:never
type Mypick<T,U extends keyof T>={
[P in U]:T[P]
}
type MyOmit<T,U>={
[P in myexclude<keyof T,U>]:T[P]
}
type MyOmit1<T,U>=Mypick<T,myexclude<keyof T,U>>
type MyType<T,> =
T extends {
a: U,
b:K
} ? [M, N] : never;
type T = MyType<{ a: string; b: number }>;
function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
const picked: Partial<T> = {};
keys.forEach((key) => {
picked[key] = obj[key];
});
return picked as Pick<T, K>;
}
const user = {
id: 1,
name: "Alice",
age: 25,
email: "[email protected]",
};
const pickedUser = pick(user, ["name", "email"]);
pickedUser.name
type case6 = Record<keyof any,any>
type StartsWith<T extends string,U extends string>=T extends `${U}${infer P}`?true:false;
type a = StartsWith<'abc', 'ab'>
type a2 = StartsWith<'abc', 'ac'>
type endsWith<T extends string,U extends string>=T extends `${infer P}${U}`?true:false;
type IndexOf<T extends any[], U, R extends any[] = []> =
T extends [infer K, ...infer Rest]
? K extends U
? R['length']
: IndexOf<Rest, U, [...R, 1,1,1,1,1,]>
: -1
type MergeValues<One, Other> =
One extends Other
? One
: Other extends unknown[]?
IndexOf<Other,One> extends -1?
[One, ...Other]
: Other
:[One, Other]
type MergeParams<
OneParam extends Record<string, any>,
OtherParam extends Record<string, any>
> = {
[Key in keyof OneParam | keyof OtherParam]:
Key extends keyof OneParam
? Key extends keyof OtherParam
? MergeValues<OneParam[Key], OtherParam[Key]>
: OneParam[Key]
: Key extends keyof OtherParam
? OtherParam[Key]
: never
}
type ParseParam<Param extends string> =
Param extends `${infer Key}=${infer Value}` //a 1=>{a:1}
? {
[p in Key]: Value
} : Param extends '' ? {} : { [K in Param]: true }
// Record<string, any>;
type ParseQueryString<Str extends string>
= Str extends `${infer Param}&${infer Rest}`
? MergeParams<ParseParam<Param>, ParseQueryString<Rest>>
: ParseParam<Str>; //'a=1' {a:1}
function parseQueryString<T extends string>(queryStr:T): ParseQueryString<T> {
if (!queryStr || !queryStr.length) {
return {} as ParseQueryString<T>;
}
const queryObj = {};
const items = queryStr.split('&');
items.forEach((item) => {
const [key, value] = item.split('=');
if (queryObj[key]) {
if(Array.isArray(queryObj[key])) {
queryObj[key].push(value);
} else {
queryObj[key] = [queryObj[key], value]
}
} else {
queryObj[key] = value;
}
});
return queryObj as any;
}
const attend = parseQueryString('a=2&a=3&a=2&b=3&1=4&d=5&e')
// attend.
type C =ParseQueryString<'a=2&a=3&a=2&b=3&true=4&d=5&e'>