Typescript 学习笔记(一)— 基础知识

Typescript 学习笔记(一)

基础知识

  1. enum 枚举
     enum Color {Red = 1, Green, Blue}
    
  2. type, interface
  3. 联合类型 |
  4. 交叉类型 &
  5. typeof
    获取一个变量声明或对象的类型
  6. keyof
    获取一个对象中的所有 key 值
    interface Person {
        name: string;
        age: number;
    }
    type K1 = keyof Person; // "name" | "age"
    
  7. in
    遍历枚举类型
    type keys = 'a' | 'b' | 'c';
    type obj = {
        [p in keys]: any
    } // {a: any, b: any, c: any}
    
  8. extends
    继承
    interface a {
        name: string;
        age: number
    }
    interface b extends a {
        sex: string;
    }
    
  9. Partial
    Partial 将某个类型的属性全都变为可选项
  10. Required
    将某个类型的属性全都变为必选项
  11. Readonly
    将某个类型的属性全都变为只读,不能被重新赋值
  12. Record
    Record<K extends keyof any, T> 将 K 中的所有属性的值转为 T 类型
    interface PageInfo {
        title: string;
    }
    type Page = 'home' | 'about' | 'contact';
    
    const x: Record<Page, PageInfo> = {
        home: { title: 't1' },
        about: { title: 't1' },
        contact: { title: 't1' }
    }
    
  13. Exclude
    Exclude<T, U> 将某个类型中属于另一个的类型移除
    type T = Exclude<'a' | 'b' | 'c', 'a'>; // 'b' | 'c'
    
  14. Extract
    Extract<T, U> 从 T 中提取 U
    type T = Extract<'a' | 'b', 'a' | 'f'>; // 'a'
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值