typescript语法细节

1.联合类型

  1. typescript的类型系统允许我们使用多种运算符,从现有类型中构建新类型
  2. 联合类型(Union Type)
    1. 联合类型是由两个或者多个其他类型组成的类型
    2. 表示可以是这些类型中的任何一个值
    3. 联合类型中的每一个类型被称之为联合成员(union‘s members)
let foo: number | string = 'abc'
foo = 123

function printID(id: number | string) {
  if (typeof id == 'string') {
    console.log(id.length)
  } else {
    console.log(id)
  }
}
printID(123)
printID('abc')

2.类型别名

// 类型别名:type
type MyNumber = number
const age: MyNumbe

 - [ ] List item

r = 18
type IdType = number | string
function printID(id: IdType) {
  console.log(id)
}
printID(123)

3.接口的声明

  1. type和interface中的区别
    1. type类型使用范围更广,接口类型只能用来声明对象
    2. 在声明对象时,interface可以多次声明
      1. type不允许两个相同的名称的别名同时存在
      2. interface可以多次声明同一个接口名称
    3. interface支持继承
    4. interface可以被类实现
  2. 总结
    1. 如果是非对象类型定义使用type,如果是对象类型的声明那么使用interface
// 1.区别一:type类型使用范围更广,接口类型只能用来声明对象
type MyNumber = number
type IdType = number | string
// 2.区别二:在声明对象时,interface可以多次声明
// 2.1 type不允许两个相同的名称的别名同时存在
type PointType1 = {
  x: number
  y: number
}
type PointType1 = {
  z?: number
}

// 2.2. interface可以多次声明同一个接口名称
interface PointType {
  x: number
  y: number
}
interface PointType {
  z?: number
}
const point: PointType = {
  x: 10,
  y: 10,
  z: 200,
}

// 3.interface支持继承
interface IPerson {
  name: string
  age: number
}
interface IKun extends IPerson {
  kouhao: string
}

const ikun1: IKun = {
  kouhao: '你干嘛,哎呦',
  name: 'kobe',
  age: 30,
}

// 4.interface可以被类实现
class Person implements IPerson {}

// 总结:如果是非对象类型定义使用type,如果是对象类型的声明那么使用interface 

4.交叉类型(Intersection Types)

  1. 交叉类似表示需要满足多个类型的条件
  2. 交叉类型使用 & 符号
// 交叉类型:两种(多种)类型要同时满足
type NewType = number & string

interface IKun {
  name: string
  age: string
}

interface ICoder {
  name: string
  coding: () => void
}
const info: IKun & ICoder = {
  name: 'why',
  age: '18',
  coding: function () {
    console.log('coding')
  },
}

5.类型断言 as

  1. 断言的规则
    1. 断言只能断言成更加具体的类型,或者不太具体(any/unknow)类型
    2. TS类型检测来说是正确的,但是这个代码本身不太正确
// 使用类型断言
const imgEl = document.querySelector('.img') as HTMLImageElement
imgEl.src = 'xxx'
imgEl.alt = 'yyy'

// 类型断言的规则:断言只能断言成更加具体的类型,或者不太具体(any/unknow)类型
const age: number = 28
const age2 = age as number
// TS类型检测来说是正确的,但是这个代码本身不太正确
const age3 = age as any
const age4 = age3 as string

6.非空类型断言!

​ 非空类型断言使用的是!,表示可以确定某个标识符是有值的,跳过ts在编译阶段对它的检测

// 定义接口
interface IPerson {
  name: string
  age: number
  friend?: {
    name: string
  }
}
const info: IPerson = {
  name: 'why',
  age: 18,
}
// 访问属性:可选链:?
console.log(info.friend?.name)
// 属性赋值:
//  解决方案一:类型缩小
if (info.friend) {
  info.friend.name = 'kobe'
}
// 解决方案二:非空类型断言(有点危险,只有确保friend一定有值的情况,才能使用)
info.friend!.name = 'kobe'

7、字面量类型的基本使用

// 1.字面量的基本使用
const name = "why";
let age: 18 = 18;
// 2.将多个字面量类型联合起来
type Direction = "left" | "right" | "up" | "down";
const d1: Direction = "left";

// 例子:封装请求方法
type MethodType = "get" | "post";
function request(url: string, method: MethodType) {}
request("http://codercba.com/api/aaa", "get");

// TS细节
const info = {
  url: "xxxx",
  method: "post",
};
// 下面的做法是错误:info.method获取的是string类型
// request(info.url, info.method);

// 解决方案一:info.method进行类型断言
request(info.url, info.method as "post");

// 解决方案二:直接让info对象类型是一个字面量类型
const info1: { url: string; method: "post" | "get" } = {
  url: "xxxx",
  method: "post",
};
request(info1.url, info1.method);

const info2 = {
  url: "xxxx",
  method: "post",
} as const;
// xxxx本身就是string类型
request(info2.url, info2.method);

8、类型缩小

  1. 什么是类型缩小
    1. 类型缩小的英文是Type Narrowing
    2. 我们可以通过类似于 typeof padding===‘number’的判断语句,来改变Typescript的执行方法
    3. 在给定的执行路径中,我们可以缩小比声明时更小的类型,这个过程称之为缩小(Narrowing)
    4. 而我们编写的typeof padding===‘number’可以称之为类型保护(type guards)
  2. 常见的类型保护
    1. typeof
    2. 平等缩小(比如===、!==)
    3. instanceof
    4. in
    5. 等等
// 1.typeof使用最多的
function printID(id: number | string) {
  if (typeof id == "string") {
    console.log(id.length, id.split(""));
  } else {
    console.log(id);
  }
}

// 2.平等缩小 ===/!==:方向类型判断
type Direction = "left" | "right" | "up" | "down";
function switchDirection(direction: Direction) {
  if (direction == "left") {
    console.log("左", "角色向左移动");
  } else if (direction == "right") {
    console.log("右", "角色向右移动");
  } else if (direction == "up") {
    console.log("上", "角色向上移动");
  } else if (direction == "down") {
    console.log("下", "角色向下移动");
  }
}

// instanceof: 传入一个日期,打印日期
function printDate(date: string | Date) {
  if (date instanceof Date) {
    console.log(date.getTime());
  } else {
    console.log(date);
  }
}

//in: 判断是否有某一个属性
interface ISwim {
  swim: () => void;
}
interface IRun {
  run: () => void;
}
function move(animal: ISwim | IRun) {
  if ("swim" in animal) {
    animal.swim();
  } else if ("run" in animal) {
    animal.run();
  }
}
const fish: ISwim = {
  swim: function () {},
};
const dog: IRun = {
  run: function () {},
};
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码下载:完整代码,可直接运行 ;运行版本:2022a或2019b或2014a;若运行有问题,可私信博主; **仿真咨询 1 各类智能优化算法改进及应用** 生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化 **2 机器学习和深度学习方面** 卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断 **3 图像处理方面** 图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知 **4 路径规划方面** 旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化 **5 无人机应用方面** 无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配 **6 无线传感器定位及布局方面** 传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化 **7 信号处理方面** 信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化 **8 电力系统方面** 微电网优化、无功优化、配电网重构、储能配置 **9 元胞自动机方面** 交通流 人群疏散 病毒扩散 晶体生长 **10 雷达方面** 卡尔曼滤波跟踪、航迹关联、航迹融合

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值