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

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理