关于typescript:TS中的关键字总结

11次阅读

共计 2945 个字符,预计需要花费 8 分钟才能阅读完成。

最近在写 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 能够应用 implements, 然而不能够应用 extends 关键字,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
    }
  • type 能够动静计算属性,interface 则不能够

    // good
    type Keys = "小王" | "小文"
    type X = {[key in Keys]: string
    }
    // bad
    interface Y {[key in Keys]: string 
    }
    // 映射的类型可能不申明属性或办法。ts(7061)
    //“Keys”仅示意类型,但在此处却作为值应用。ts(2693)
    
    // bad
    type XX = keyof Keys;
    interface Y {[k: XX]: number
    }
    // 索引签名参数类型不能为文本类型或泛型类型。请思考改用映射的对象类型。ts(1337)
  • 两者的拓展
    请留神接口和类型别名不是互斥的。接口能够扩大类型别名,反之亦然。

    // 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 将类型字段的值批改为只读属性,禁止批改
  • 性能是将类型的属性变成可批改,意思就是去除只读
正文完
 0