TypeScript

  1. TSPick知道不?知道是咋实现的么 用于构造一个类型,它是从Type类型里面挑了一些属性Keys(Keys是字符串字面量 或者 字符串字面量的联合类型)。
  2. TS 的泛型 泛型可以简单理解为不指定类型,而是在使用的时候才指定类型
// 不一定是字母T,任何字母都可以,主要是尖括号
function test<T>(arg: T): T {
  return arg;
}
  1. TS 的 Pick,Omit,Required,Partial,Readonly
type User = {
  name: string;
  id?: string;
  age: number;
  avatar: string;
};
type Id = Pick<User, "id">;
type MustAll = Required<User>;
type MustId = Required<Pick<User, "id">>;
type ParAll = Partial<User>;
type ParName = Partial<Pick<User, "name">>;
type NameId = Required<Pick<User, "name" | "id">>;
type ParAgeAvatar = Partial<Pick<User, "age" | "avatar">>;
type ReadName = Readonly<Pick<User, "name">>;
type ReadAll = Readonly<User>;
type ReadIdAge = Readonly<Pick<User, "id" | "age">>;
type omitId = Omit<User, "id">;
type OmitNameAge = Omit<User, "name" | "age">;
type OmitPartial = Partial<Omit<User, "name">>;
type ReOmitName = Required<Omit<User, "name">>;
type ReadPO = Readonly<Partial<Omit<User, "name">>>;
  1. TS 的 keyof 和 typeof 关键字 这两个关键字是 TypeScript 类型系统中非常强大的工具,用于在类型层面进行操作和推导。 keyof 是 TypeScript 中的索引类型查询操作符,它返回一个类型的所有属性名组成的联合类型。 typeof 用于获取变量或属性的类型
interface Person {
  name: string;
  age: number;
  location: string;
}

type PersonKeys = keyof Person;
// 等价于 type PersonKeys = "name" | "age" | "location"
const person = { name: "Alice", age: 30 };
type PersonType = typeof person;

// 等同于
type PersonType = {
  name: string;
  age: number;
};

results matching ""

    No results matching ""