行业知识
TypeScript类型体操练习
2026-10-26 03:56:34
来源:互联网转载
历史小剧场
这个世界上,有两种人最痛苦,第一种是身居高位者,第二种是身居底层者,第一种人很少,第二种人很多。第一种人叫崇祯,第二种人叫百姓。
()而最幸福的,就是中间那拨人,主要工作,叫做欺上瞒下,具体特点是,除了好事,什么都办;除了脸,什么都要。—《明朝那些事儿》
字符串模板与extends/infer
- extends: 有两个作用:1、接口继承,2、类型判断(这篇文章主要是体现这个作用);
- infer: 推导泛型参数,只在extends右边使用
案例
()type str = "beyond";// 获开头type getFirst = T extends `${infer First}${infer Rest}`? First : never;type FirstLetter = getFirst; // type FirstLetter = "b"// 获取除开头的部分type getRest = T extends `${infer First}${infer Rest}` ? Rest : never;type RestLetters = getRest; // type RestLetters = "eyond"// 以y分隔type Split = T extends `${infer Front}y${infer Back}` ? Back : never;type Splitted = Split; // type Splitted = "ond"判断开头
type startsWidth = str extends `${prefix}${infer Rest}` ? true : false;type startsWidthKong = startsWidth; // type startsWidthKong = truetype StartsWithBe = startsWidth; //type StartsWithBe = truetype StartsWithC = startsWidth; // type StartsWithC = false转为首字母大写
type UppercaseFirst = T extends `${infer First}${infer Rest}` ? `${Uppercase}${Rest}` : never;type UppercaseStr = UppercaseFirst; // type UppercaseStr = "Beyond"文本替换-替换第一个
type ReplaceOne = str extends `${infer Front}${from}${infer Rest}` ? `${Front}${to}${Rest}` : str;type ReplaceStr = ReplaceOne; // type ReplaceStr = "beyand"键值对转索引 a=1 => {a:1}
type ConvertStrToRecord = str extends `${infer key}=${infer value}` ? { [k in key]: value } : never;type ConvertStrToRecordStr = ConvertStrToRecord; // type ConvertStrToIndexStr = { // a: "1";// }as 重映射-索引的重命名
索引转大写
interface IOut { aaa: 1, bbb: 2, fun: () => void}type UpperKeys = { [K in keyof T as (T[K] extends Function ? K : Uppercase)]: T[K]}type res = UpperKeys;// type res = {// AAA: 1;// BBB: 2;// fun: () => void;// }合并索引
interface IOut2 { ccc: 3, ddd: 4}type CombineIndex = { [K in keyof A | keyof B] : K extends keyof A ? A[K] : K extends keyof B ? B[K] : never}type combine = CombineIndex;// type combine = {// aaa: 1;// bbb: 2;// fun: () => void;// ccc: 3;// ddd: 4;// }巧用递归
文本替换-替换所有
type ReplaceAll = Str extends `${infer Front}${From}${infer Rest}` ? `${Front}${To}${ReplaceAll}` : Str;type ReplaceAllStr = ReplaceAll; // type ReplaceAllStr = "12AAAAAA456"字符串反转
type Reverse = Str extends `${infer First}${infer Rest}` ? ReverseFirst}${Res}` : Res;type ReverseStr = Reverse; // type ReverseStr = "olleh"综合
字符串解析-初级
例如:我们要将 a=1&b=2&c=3 转为 {a:1, b:2, c:3}
那么,我们要有按这三步走
- &分割取 键值对 处理
- 处理单个的 键值对 转换为 索引类型
- 把 转换后的 索引类型 合并
type Parse} = Str extends `${infer One}&${infer Rest}` ? Parse : CombineIndextype parseToRecord = Parse; // type parseToRecord = {a: "1", b: "2", c: "3"}字符串解析-升级
- 如果只有键没有值,则返回 {key: true};
- 合并重复索引。例如 a=1&a=2 转为 {a: [‘1’, ‘2’]};
- 合并的值不能重复。例如 a=1&a=2&a=2 转为 {a: [‘1’, ‘2’]};
type ConvertStrToRecordUp = Str extends `${infer Key}=${infer Value}` ? { [K in Key] : Value } : Str extends `${infer Key}` ? { [K in Key]: true } : {};type ConvertStrToRecordUpStr = ConvertStrToRecordUp;// type ConvertStrToRecordUpStr = {// a: true;// }// 合并重复索引type CheckDuplicate = keyof B extends keyof A ? AddR : CombineIndex;type AddR = { [K in keyof A] : K extends keyof B ? CheckInclue extends true ? A[K] : // 重复索引,合并值,值不能重复 A[K] extends any[] ? [...A[K], B[K]] : [A[K], B[K]] : A[K]}type CheckInclue = A extends [infer First, ...infer Rest] ? First extends B ? true : CheckInclue : false;type checkIncludeDemo = CheckInclue; // type checkIncludeDemo = truetype ParseUp} = Str extends `${infer One}&${infer Rest}` ? ParseUp : CheckDuplicatetype parseToRecordUp = ParseUp; // type parseToRecordUp = {// b: true;// c: true;// a: ["1", "2", "3"];// e: [true, "10"];// d: "4";// }
上一篇:怎么连接外国服务器挖矿?
下一篇:米哈游怎么删除服务器?