simplify the life

04 2023 档案

【type-challenges】第一个元素
摘要:第一个元素 链接 获取元素的第一个元素,当作类型,T[0] 即可拿到,关键是需要判空 参考 14 - First of Array >三种答案 方法一 直接特判空元祖 type First<T extends any[]> = T extends [] ? never : T[0] 方法二 拿 le 阅读全文

posted @ 2023-04-22 20:08 lessfish 阅读(85) 评论(0) 推荐(0)

extends
摘要:extends 接口继承 interface Person { name: string age: number } interface Man extends Person { height: number } 此时 Man 自动继承了 Person 下的 name 和 age 泛型约束 实现 P 阅读全文

posted @ 2023-04-21 20:09 lessfish 阅读(144) 评论(0) 推荐(0)

【type-challenges】元祖转换为对象
摘要:元祖转换为对象 链接 首先熟悉下 as const 的用法,在这里会将数组变成只读元祖 const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const // 等同于 tuple 定义为 readonly ["tesla", "mod 阅读全文

posted @ 2023-04-21 19:08 lessfish 阅读(66) 评论(0) 推荐(0)

as const
摘要:as const 当 as const 修饰符用在变量声明或表达式的类型上时,它会强制 TypeScript 将变量或表达式的类型视为不可变的(immutable) 对象属性是只读的 const bar = { x: 1, y: 2 } as const bar.x = 3 // 无法为“x”赋值, 阅读全文

posted @ 2023-04-21 17:58 lessfish 阅读(147) 评论(0) 推荐(0)

导航