组件使用
封装第三方组件 props
import { Checkbox } from "antd"; type CheckboxProps = React.ComponentProps<typeof Checkbox>; type My
lhh|07/31/2024 14:20
进阶
箭头函数写泛型
// tsx 中需要加多个 , 或者 extends,否则会识别成 react 的标签 const fn = <T>(p: T): T => { return p; }; const fn2 =
lhh|07/31/2024 14:19
进阶
读取对象数组中的对象类型
type Person = { name: string; age: number; schools: { id: string }[] }; type PersonSchool = Person["
lhh|07/31/2024 14:19
进阶
读取对象的 key 作为 type
const person = { name: "lhh", age: "18" }; type PersonKey = keyof typeof person; type Person = { [ke
lhh|07/31/2024 14:19
进阶
将类型的所有可选属性转为必选
/** 将类型的所有可选属性(包括嵌套的,同时移除null)都转化为必选 */ type DeepRequired<T> = { [P in keyof T]-?: T[P] extends ob
lhh|07/31/2024 14:18
进阶
将指定 key 变为必选
type RequireKey<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: T[P] }; // type RequireKey2<T, K
lhh|07/31/2024 14:18
进阶
将指定 key 变为可选
type WithPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; type Person = { id: str
lhh|07/31/2024 14:18
基础
Record
映射属性,即下面属性全部映射为 stringtype PersonKey = "name" | "age"; type PersonType = Record<PersonKey, string>;
lhh|07/31/2024 14:17
基础
Partial Required
type PersonType = { name: string; age: string; }; type PersonType2 = Partial<PersonType>; type _
lhh|07/31/2024 14:16
基础
Extract Exclude
type T1 = Extract<"a" | "b" | "c", "a" | "f">; type T11 = "a"; // 等效于 type T2 = Exclude<"a" | "b" |
lhh|07/31/2024 14:16