Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

    • 基础

      • TypeScript介绍
      • TypeScript基础类型
      • TypeScript对象类型
      • TyprScript类型推论
      • TypeScript高级类型
        • TypeScript高级类型
          • 交叉类型
          • 联合类型
          • 类型别名
          • 类型保护
      • TypeScript接口
      • TypeScript类型别名与接口的区别
      • TypeScript泛型
      • TypeScript项目配置
    • 实例

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • TypeScript
  • 基础
HiuZing
2022-09-27
目录

TypeScript高级类型

# TypeScript高级类型

# 交叉类型

用&操作符表示,交叉类型就是两个类型必须存在

person 即是 IpersonA 类型,又是 IpersonB 类型

interface IpersonA{
  name: string,
  age: number
}
interface IpersonB {
  name: string,
  gender: string
}

let person: IpersonA & IpersonB = { 
    name: "宝贝",
    age: 18,
    gender: "女"
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14

注意:

交叉类型取的多个类型的并集,但是如果key相同但是类型不同,则该key为never类型

interface IpersonA {
    name: string
}

interface IpersonB {
    name: number
}

function testAndFn(params: IpersonA & IpersonB) {
    console.log(params)
}

testAndFn({name: "宝贝"}) // error TS2322: Type 'string' is not assignable to type 'never'.
1
2
3
4
5
6
7
8
9
10
11
12
13

# 联合类型

联合类型用|分隔,表示取值可以为多种类型中的一种

let status:string|number
status='to be or not to be'
status=1
1
2
3

# 类型别名

类型别名用来给一个类型起个新名字。它只是起了一个新名字,并没有创建新类型。类型别名常用于联合类型

type count = number | number[];
function hello(value: count) {}
1
2

# 类型保护

类型保护是可执行运行时检查的一种表达式,用于确保该类型在一定的范围内

主要有四种的方式来实现类型保护:

  • in 关键字

    interface InObj1 {
        a: number,
        x: string
    }
    interface InObj2 {
        a: number,
        y: string
    }
    function isIn(arg: InObj1 | InObj2) {
        // x 在 arg 打印 x
        if ('x' in arg) console.log('x')
        // y 在 arg 打印 y
        if ('y' in arg) console.log('y')
    }
    isIn({a:1, x:'xxx'});
    isIn({a:1, y:'yyy'});
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
  • typeof类型保护

    typeof 只支持:typeof 'x' === 'typeName' 和 typeof 'x' !== 'typeName',x 必须是 'number', 'string', 'boolean', 'symbol'。

    function isTypeof( val: string | number) {
      if (typeof val === "number") return 'number'
      if (typeof val === "string") return 'string'
      return '啥也不是'
    }
    
    1
    2
    3
    4
    5
  • instanceof类型保护

    function creatDate(date: Date | string){
        console.log(date)
        if(date instanceof Date){
            date.getDate()
        }else {
            return new Date(date)
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 自定义类型保护

    function isNumber(num: any): num is number {
        return typeof num === 'number';
    }
    function isString(str: any): str is string{
        return typeof str=== 'string';
    }
    
    1
    2
    3
    4
    5
    6

具体来源 (opens new window)

#TS
上次更新: 2024/08/14, 04:14:33
TyprScript类型推论
TypeScript接口

← TyprScript类型推论 TypeScript接口→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式