美文网首页
TS中几种函数重载

TS中几种函数重载

作者: wyc0859 | 来源:发表于2022-03-18 00:02 被阅读0次

函数重载

函数重载,先要了解什么是函数签名:
函数签名 = 函数名称+函数参数+函数参数类型+返回值类型四者合成,包含了实现签名和重载签名
function funa(value: number, type: string): string 这就是函数签名(重载签名)

type MessageType = "order" | "pay" | string; //微信消息类型
type Message = {
  id: number;
  type: MessageType;
  msg: string;
};

let messages: Message[] = [
  {
    id: 1,
    type: "order",
    msg: "订单金额错误",
  },
  {
    id: 2,
    type: "pay",
    msg: "支付失败",
  },
  {
    id: 3,
    type: "order",
    msg: "订单无效",
  },
  {
    id: 4,
    type: "pay",
    msg: "支付回调错误",
  },
];

函数重载由一个实现签名 + 一个或多个重载签名合成

function getMessage(value: number): Message; //重载签名1
function getMessage(value: MessageType, count: number): Message[]; //重载签名2
//实现签名↓
//参数类型也可以定义为: any 或 unknown(和any一样都是顶级类型)
function getMessage(value: number | MessageType, count: number = 1) {
  //count: number = 1 必须要加这个默认值,因为签名1没有这个参数,函数体中却有,会报错
  if (typeof value === "number") {
    return messages.find((msg) => {
      return value === msg.id;
    });
  } else {
    //过滤后得到多条,在splice确定返回条数
    return messages.filter((msg) => value === msg.type).splice(0, count);
  }
}

const detail = getMessage(1);
const list = getMessage("pay", 2); //签名定义了必须2个参数
console.log("1:", detail.msg); //这样直接点即可操作,不需要在(<>)转换了

list.forEach((item) => {
  console.log("2:", item.msg);
});

构造函数重载

type people = {
  name: string;
  age: number;
  sex?: string;
};
const san: people = {
  name: "山姆",
  age: 18,
  sex: "男",
};
console.log("type类型:", typeof san); //object

class Love {
  public name: string;
  public age: number;
  public sex: string;

  constructor(name_: string, age_: number);
  constructor(param: people);
  //由于构造函数本身不返回任何值,默认this且隐藏,所以重载和实现签名都不返回
  constructor(nameOrParam: any, age_: number = 0) {
    if (typeof nameOrParam === "object") {
      this.name = nameOrParam.name;
      this.age = nameOrParam.age;
      this.sex = nameOrParam.sex;
    } else {
      this.name = nameOrParam;
      this.age = age_;
      this.sex = "未知";
    }
  }

  public show(): string {
    let str = `这是一位${this.age}的${this.sex},名叫${this.name}`;
    console.log(str);
    return str;
  }
}

const a = new Love("小丽", 18);
const b = new Love(san);
a.show(); //这是一位18的未知,名叫小丽
b.show(); //这是一位18的男,名叫山姆

export {};

相关文章

  • TS中几种函数重载

    函数重载 函数重载,先要了解什么是函数签名:函数签名 = 函数名称+函数参数+函数参数类型+返回值类型四者合成,包...

  • Redux源码阅读_3

    combineReducers.ts 函数重载声明 首先是对combineReducers函数的重载,重载了三个函...

  • Redux源码阅读_4

    applyMiddleware.ts 函数重载声明 首先是对applyMiddleware函数的重载,重载了七个函...

  • TS 函数重载

    函数重载 这个概念是在一些强类型语言中才有的,在JS中依据不同参数类型或参数个数执行一些不同函数体的实现很常见,依...

  • 02|typescript常用实例

    一、定义索引数组 二、定义函数 函数参数可选 函数参数默认值 函数参数的剩余变量 函数的重载 三、ts中的类 类的...

  • js的重载

    问:什么是重载?答:同样的函数,不同样的参数个数。《JS高级程序设计》里是提到过函数是没有重载的,ts中有重载。但...

  • TypeScript 学习笔记3 函数

    1.函数定义 1.1 js 函数定义 1.2 ts 函数定义 2.函数重载 Typescript从0到1-学习视频...

  • 5.typeScript中函数相关知识点梳理。

    函数定义 函数重载 ts编译器在处理重载的时候,会去查询一个重载的列表,并且会尝试第一个定义,如果匹配的话就是用这...

  • typeScript学习02

    typescript中的函数 ts中函数定义 ts中函数的传参 ts中的函数的可选参数(js中函数的形参和实参可以...

  • [进阶]C++:函数重载

    定义重载函数 重载函数是函数名相同但是参数列表不同的函数 重载和const形参 const_cast 在重载函数中...

网友评论

      本文标题:TS中几种函数重载

      本文链接:https://www.haomeiwen.com/subject/rozudrtx.html