个人博客 个人博客
首页
  • 前端
  • 后端
  • Git
  • Docker
  • 网络
  • 操作系统
工具
阅读
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

董先亮

前端react开发
首页
  • 前端
  • 后端
  • Git
  • Docker
  • 网络
  • 操作系统
工具
阅读
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

  • JavaScript

  • TypeScript

    • interface 与 type
    • 类型
      • 元组
      • 联合类型
      • 交叉类型
      • 泛型
    • 工具类
    • tsconfig详解
  • 前端基础
  • TypeScript
NeverStop1024
2022-06-19
目录

类型

# 元组

元组与数组相似,数组每一项可以是注解中的任意一个,元组则是数组每个位置必须与同位置注解类型相同。开发中不常用。

// 数组 某个位置的值可以是注解中的任何一个
const LOL: (string | number)[] = ["zed", 25, "darts"];

// 元组 每一项数据类型必须一致
const LOL: [string, string, number] = ["zed", "darts", 25];
1
2
3
4
5

# 联合类型

|表示,满足其一即可

interface Waiter {
  anjiao: boolean;
  say: () => {};
}

interface Teacher {
  anjiao: boolean;
  skill: () => {};
}

// 联合类型
function judgeWho(animal: Waiter | Teacher) {}
1
2
3
4
5
6
7
8
9
10
11
12

# 交叉类型

&表示,会对类型进行合并。

type TOne = {x: number};
type TAll = TOne & {y: string};

// 相当于
interface TAll {
  x: number
  y: string
}
1
2
3
4
5
6
7
8

注意点

  1. 同名基础类型进行合并, 会变为never
interface A {
    name: string
}

interface B {
    name: number
}
type AB = A & B

// 相当于
interface AB {
    name: never
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  1. 同名非基础类型,可正常进行合并
interface A {
    a: {x: string}
}

interface B {
    a: {
        y: string
    }
}
type AB = A & B

// 相当于
interface AB {
  a: { 
      x: string,
      y: string
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  1. 同名方法类型合并
    • 目前还不知道,定义后该如何使用
interface A {
  name: string
  fun: (name: string) => void
}
interface B {
  name: number
  fun: (name: number, age: number) => void
}
type AB = A & B

const name: AB = {
    name: 's',
  fun: (name: number, age: number) => {}
}
// 错误提示
// Type '(name: number, age: number) => void' is not assignable to type '((name: string) => void) & ((name: number, age: number) => void)'.
// Type '(name: number, age: number) => void' is not assignable to type '(name: string) => void'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 泛型

  1. 箭头函数使用泛型
const foo = <T,>(x: T): T => x;

const foo = <T extends {}>(x: T): T => x;

const foo = <T extends Record<string, unknown>>(x: T): T => x;

const foo: <T>(x: T) => T = x => x;

const identity = <T,>(arg: T): T => {
    console.log(arg);
    return arg;
};

const renderAuthorize = <T>(Authorized: T): ((currentAuthority: CurrentAuthorityType) => T) => (
    currentAuthority: CurrentAuthorityType,
  ): T => {
     return
 };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  1. 普通函数
// T 自定义名称
function myFun<T>(params: T[]) {
  return params;
}
myFun <string> (["123", "456"]);

// 定义多个泛型
function join<T, P>(first: T, second: P) {
  return `${first}${second}`;
}
join <number, string> (1, "2");
1
2
3
4
5
6
7
8
9
10
11
  1. 泛型接口
interface Cart<T> {
  list: T[];
}
let cart: Cart<{ name: string; price: number }> = {
  list: [{ name: "hello", price: 10 }],
};
console.log(cart.list[0].name, cart.list[0].price);
1
2
3
4
5
6
7
编辑 (opens new window)
上次更新: 2022/06/19
interface 与 type
工具类

← interface 与 type 工具类→

最近更新
01
mock使用
07-12
02
websocket即时通讯
07-12
03
前端面试题
07-09
更多文章>
Theme by Vdoing | Copyright © 2022-2023 NeverStop1024 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式