1.函数

1.1 重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function add(num1: number, num2: number): number; // 没函数体
function add(num1: string, num2: string): string;

function add(num1: any, num2: any): any {
if (typeof num1 === 'string' && typeof num2 === 'string') {
return num1.length + num2.length
}
return num1 + num2
}

const result = add(20, 30)
const result2 = add("abc", "cba")
console.log(result)
console.log(result2)

2.Type

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
/**
type A = {
aa:string
Bb:string
cc_Dd:strng
}

type B = {
aa:string
bb:string
ccDd:string
}
写一个camel类型完成这种转换
*/
type A = {
aa: string
Bb: string
cc_Dd: string
cc_dd_ee: boolean
}

type CamelCase<Str extends string> =
Str extends `${infer left}_${infer rest}` ?
`${left}${rest extends `${infer left1}_${infer rest2}` ? CamelCase<Capitalize<string & rest>> : Capitalize<string & rest>}`
: Lowercase<string & Str>

type camel<obj> = {
[key in keyof obj as CamelCase<string & key>]: obj[key]
}

type res = camel<A>