TypeScript 关键字总结

388 阅读3分钟

最近在写ts,偶尔会遇到一些之前没见过的一些符号或者关键词,索性来一次全面筛查,相似或有关联的都放在一起总结了!

is

is 类型保护,用于判断类型的函数中做类型限制

// bad
function isString(value: unknown): boolean{
  return typeof value === "string";
}

//good
function isString(value: unknown): value is string{
  return typeof value === "string";
}

in

in 其实就像是遍历一样

type Keys = 'a' | 'b' | 'c';
type obj = { 
    [ T in Keys]: string;
}
// in 遍历 Keys,并为每个值赋予 string 类型
 
// type Obj = {
//     a: string,
//     b: string,
//     c: string
// }

keyof

keyof 可以获取一个对象接口的所有 key值

type obj = { a: string; b: string }
type Foo = keyof obj;
// type Foo = 'a' | 'b';

typeof

typeof 用于获取某个变量的具体类型

const obj = { a: '1' };
type Foo = typeof obj; 
// type Foo = { a: string }

extends、implements

  • extends用于接口与接口、类与类、接口与类之间的继承
  • implements用于类与类、类与接口之间的实现

注意: extends类似于es6的extends,implements没有继承效果的,但是要求子类上必须需有父类的属性和方法,更倾向于限制子类的结构!

type、interface

type 类型别名 用于给类型起一个新名字

type Value: string = "111"
// 或
type Iprops = {
  value: string,
  getName: () => string
}

interface 接口 用于声明类型

interface MyInterface {
  value: string,
  getName: () => string
}

type 和 interface的区别

  • type可以定义单个变量类型、联合类型、对象,interface只能定义对象;
  • type不可以重复声明,interface可以重复声明(声明合并);
type Iprops = {
  name: string
  age: number
}
type Iprops:string = "111" // Error 标识符“Iprops”重复。ts(2300)

interface MyInterface {
  name: string
  age: number
}
interface MyInterface {
  gender: string
}
const obj: MyInterface= {
  name: "string"
  age: 18,
  gender: "男",
}
// 重复声明相当于把两个类型加一块

注意: 重复声明同字段若类型不同也会报错

interface MyInterface {
  name: string
  age: number
}
interface MyInterface {
  age: string // 后续属性声明必须属于同一类型。属性“age”的类型必须为“number”,但此处却为类型“string”。ts(2717)
  gender: string
}
  • 两者的拓展 请注意接口和类型别名不是互斥的。接口可以扩展类型别名,反之亦然。
// interface
interface PartialPointX { x: number; }
interface Point extends PartialPointX { y: number; }

// type
type PartialPointX = { x: number; };
type Point = PartialPointX & { y: number; };

infer

infer用于提取属性,具体的返回类型是依据三元表达式的返回而定。

type myInter<T> = T extends Array<infer U> ? U : T

Pick

用于在定义好的类型中取出特性的类型

interface UserInfo {
  id: string;
  name: string;
}
type NewUserInfo = Pick<UserInfo, 'name'>; // {name: string;}

Record

Record 可以获得根据 K 中所有可能值来设置 key 以及 value 的类型

interface UserInfo {
  id: string;
  name: string;
}
type CurRecord = Record<'a' | 'b' | 'c', UserInfo>; // { a: UserInfo; b: UserInfo; c: UserInfo; }

ReturnType

ReturnType 用来获取函数的返回值的类型

type Func = (value: number) => string;
const foo: ReturnType<Func> = "1";

Partial、DeepPartial、Required

  • Partial 功能是将类型的属性变成可选
interface UserInfo {
  id: string
  name: string
}
// bad
const machinist: UserInfo = {
  name: 'machinist'
} 
// error 类型 "{ name: string; }" 中缺少属性 "id",但类型 "UserInfo" 中需要该属性。ts(2741)

type NewUserInfo = Partial<UserInfo>;
// good
const machinist: UserInfo = {
  name: 'machinist'
}

注意: Partial只支持处理第一层的属性,如果想要处理多层,可以使用DeepPartial,使用方法与Partial相同这里就不举例了

  • Required 功能与Partial相反,将类型的属性变成必选
interface UserInfo {
  id?: string
  name?: string
}
type newUserInfo =  Required<UserInfo>
const machinist: newUserInfo = {
 id:"111"
}
// error 类型 "{ id: string; }" 中缺少属性 "name",但类型 "Required<UserInfo>" 中需要该属性。ts(2741)

Readonly、Mutable

  • Readonly将类型字段的值修改为只读属性,禁止修改
  • 功能是将类型的属性变成可修改,意思就是去除只读

推荐

推荐一个学习在react中使用typescript的资料,传送门:typescript-cheatsheets