我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

    • nodejs

    • git

    • CI

    • 小程序

    • docker

    • Typescript

      • 说说Typescript中命名空间和模块的区别
      • 说说Typescript中的数据类型有哪些
      • 说说什么时候使用枚举类型
      • 说说如何在React项目中使用Typescirpt
      • 说说如何在Vue项目中使用Typescirpt
      • 说说对Typescript中函数的理解
      • 说说对Typescript中接口interface的理解
      • 说说对Typescript中泛型的理解
      • 说说对Typescript中类的理解
      • 说说对Typescript中高级类型的理解
        • 是什么
        • 有哪些
          • 交叉- 并
          • 联合类型 - 或
          • 类型别名 - 新名字
          • 类型索引 - keyof
          • 类型约束 - extends
          • 映射类型 - in
          • 条件类型 - ?
        • 总结
      • 说说对Typescript的理解
    • webpack

    • 安全

  • 基础

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 大前端
  • Typescript
fangdown
2021-09-15
目录

说说对Typescript中高级类型的理解

# 是什么

高级类型: 除基础类型(string,number 等)之外的类型应用

# 有哪些

  • 交叉类型
  • 联合类型
  • 类型别名
  • 类型索引
  • 类型约束
  • 映射类型
  • 条件类型

# 交叉- 并

T & U

function extend<T , U>(first: T, second: U) : T & U {
  let result: <T & U> = {}
  for (let key in first) {
      result[key] = first[key]
  }
  for (let key in second) {
      if(!result.hasOwnProperty(key)) {
          result[key] = second[key]
      }
  }
  return result
}

# 联合类型 - 或

T | U;
function fn(str: string[] | string) {}

# 类型别名 - 新名字

type do = boolean | string
type Tree<T> = {
  value: T
}

# 类型索引 - keyof

interface IType {
  name: string;
  age: number
}
type myType = keyof IType
type myType = 'name' | 'age'

# 类型约束 - extends

function getValue<T, K extends keyof T>(obj: T, key: K ){
  return obj[key]
}

# 映射类型 - in

type Readonly<T> = {
  readonly [P in keyof T]: T[P];
};

# 条件类型 - ?

T extends U ? X : Y

# 总结

在复杂场景下,需要使用高级类型的频率就越多

#ts
上次更新: 2021/12/19, 18:05:42
说说对Typescript中类的理解
说说对Typescript的理解

← 说说对Typescript中类的理解 说说对Typescript的理解→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式