首页 > 其他分享 >TypeScript--高级用法

TypeScript--高级用法

时间:2022-11-25 15:13:04浏览次数:58  
标签:function TypeScript string -- number 用法 interface type Bird

TypeScript--高级用法

1. 运算符

  • 可选链运算符 ?.
    判断左侧的表达式 是否是 null或者 undefined ,如果是,则会停止表达式的运行,减少我们大量的 && 运算
obj?.prop
obj?.[index]
func?.(args)
  • 非空断言运算符 !
    强调对应的元素不是null 和 undefined
function onClick(callBack?: () => void) {
  callBack!()
}
let a: {foo?: string} = {foo: 'foo'}
let b = a!.foo

上面的列子中 callBack 和 foo 是可选, 在使用的时候 使用 !来告诉编译器 他是非空的

  • 空值合并运算符 ??
    ?? 和 js 中 || 比较类似,区别在于 ?? 只有左侧表达式为 null 或者 undefined 才会返回 右侧表达式。 || 左侧表达式 为 0 false 等 都会返回右侧表达式
const type = option.type?? 'type1'
  • 模板字面量类型
type word = 'word'
type helloword = `hello ${word}`
  • 联合类型
type T1 = 'header' | 'footer'
type T2 = `${T1}_id`  // header_id footer_id
  • 内置字符操作类型
    Uppercase 将小写 转为大写
    Lowercase 将大写 转为小写
    Capitalize 将第一个字符转为大写
    Uncapitalize 将第一个字符转为小写
type Greeting = 'Hello word'
type ShoutGreeting = Uppercase<Greeting>  // HELLO WORD

type QuiteGreeting = Lowercase<Greeting> // hello word

2. 类型守卫

是可执行运行时检查的一种表达式, 用于确保该类型在一定范围内,换句话就是 类型首位可以保证一个字符串是一个字符串,尽管他的值 也可能是个 数值
类型守卫的思想是 尝试检测属性,方法 或者原型,以确定如何处理值

typeof 关键字

function padLeft(value: string; padding: string | number) {
  if (typeof padding === 'number') {
    // 处理 number
  }
  if (typeof padding === 'string') {
    // 处理 string
  }
}

instanceof 关键字

判断一个对象是否是某个类的实例

in 关键字

使用的情况 和js 中的使用 是相同的

interface Admin {
  name: string;
  privi: string;
}
interface Empolee {
  name: string;
  startData: Date
}
type UnknowEmpolee = Admin | Empolee
function print (emp: UnknowEmpolee) {
  if ('startData' in emp) {
    console.log('.....')
  }
}

使用switch case 来进行可辨识联合

interface One {
  type: 'one';
  num: 98;
}
interface Two {
  type: 'two';
  nums: 98;
}
interface Three {
  type: 'three';
  numss: 98;
}
let classNum = One | Two | Three;
function sum (option: classNum) {
  switch (option.type) {
    case: 'one':
      return option.num * 9;
    case: 'two':
      return option.nums * 9
    case: 'three':
      return option.numss * 9
  }
}

类型谓词 is

先看下没有用 is 这个谓词的时候的写法

function isBird(bild: Bird | Fish): boolean {
  return !!(bird as Bird).fly
}
function isFish(fish: Bird | Fish): boolean {
  return !!(fish as Fish).swim
}
function start(pet: Bird | Fish) {
  if (isBird(pet)) {
    (pet as Bird).fly()
  }else if (isFish(pet)) {
    (pet as Fish).swim()
  }
}

这里是使用 is 这个谓词的写法

function isBird(bild: Bird | Fish): bird is Bird {
  return !!(bird as Bird).fly
}
function start(pet: Bird | Fish) {
  if (isBird(pet)) {
    Bird.fly()
  }else if (isFish(pet)) {
    Fish.swim()
  }
}

在这里定义 isBird 的返回值时, bird is Bird 也是一个 boolean 值, 但是多了一层含义就是 bird 就是 Bird 这个类型的值
这样在start 中我们使用的时候就不需要 写类型断言了

3. 声明合并

函数合并

function add (a: number, b: number): number
function add (a: string, b: string): string
function add (a: string, b: number): string
function add (a: number, b: string): string
function add (a: Combinable, b: Combinable) {
  if (typeof a == 'string' || typeof b == 'string') {
    return a.toString() + b.toString()
  }
  return a + b
}

重载时,一定要把最精确的定义放在最前面,因为编译时会查找重载列表,尝试使用第一个重载定义。

接口合并

interface Alarm {
  price: number;
}
interface Alarm {
  weight: number;
}
等价与
interface Alarm {
  price: number;
  weight: number;
}

这里需要注意的时, 接口合并的时候, 同一个属性的类型不能前后定义不一致

4. 申明文件 .d.ts

主流的库都是 js 编写的,不支持类型系统,这个时候需要编写包含类型注释的 .s.ts 文件, 便可以在使用js 库的时候,获得静态类型检查
全局变量的申明写法

declare var 声明全局变量
declare function 申明全局方法
declare class 申明全局类
declare enum 申明全局枚举
declare namespace 申明有子属性的全局对象
interface type 申明全局类型

全局模块 和本地模块

区分两种模块 就看文件中有没有 import export ,有这两个关键字就是本地模块,使用的时候需要导入

自动生成声明文件

在ts.config.json中 设置 declaration为 true 这样就会为你自动生成 申明文件

{
  compilerOptions: {
    declaration: true
  }
}

Definitely Typed 这个项目中有写好的 申明文件, 用的时候,可以在这个项目中去下载

下面是我的小程序体验码,希望能和大家共同学习进步

标签:function,TypeScript,string,--,number,用法,interface,type,Bird
From: https://www.cnblogs.com/eyesstar/p/16925204.html

相关文章

  • 云享·人物丨造梦、探梦、筑梦,三位开发者在华为云上的寻梦之旅
    摘要:走近华为云开发者日HDC.CloudDay,看三位特别的开发者用技术改变世界,用创造力让生活更美好。本文分享自华为云社区《云享·人物丨造梦、探梦、筑梦,三位开发者在华为云......
  • TypeScript - -类型实战
    TypeScript--类型实战下面介绍的几个常见实战操作,数量不多,但是提供了一些思路,学习理解这些思路,和js实现的区别。为自己写代码的时候打下小小的基础1.实现返回promi......
  • postMan 测试webService接口 参数传递问题
    一、参数没有子节点的方式<?xmlversion="1.0"encoding="utf-8"?><soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.......
  • js中对数字数组排序
    js中对数组数字排序(3条消息)js实现数字排序方法_soupJian前端养成记的博客-CSDN博客_js数字排序<scripttype="text/javascript">functionweek(){va......
  • Java ArrayList移除元素相关
    @TestpublicvoidtestList(){List<String>list=newArrayList<>();list.add("1");list.add("2");for(Strings:lis......
  • Jenkins学习
    Jenkins流水线是用于实现和集成持续交付(ContinuousDelivery,CD)的一系列插件集合,Jenkins流水线可以写入一个叫做JenkinsFile的文本文件中从而能被纳入版本控制中。目前Jenk......
  • Linux部署kafka集群
    Linux部署kafka集群(亲测有效)原创 陈肖萧晓 若愚Linux 2022-11-2308:00 发表于山东收录于合集#linux7个#kafka1个#集群1个Linux部署kafka(亲测有效)网上有太......
  • TypeScript--5常见问题
    TypeScript--5常见问题interface和type的区别是什么||interface|type||---|---|---||可定义类型|object,array,function,classconstructor|所......
  • matlab工具箱TTSBOX源码中文分析
    functionwav=tts(txt,voice,pace,fs)%TTStexttospeech.%TTS(TXT)synthesizesspeechfromstringTXT,andspeaksit.Theaudio%formatismono,16bit,1......
  • 【iOS开发必备指南合集】申请企业级IDP、真机调试、游戏接入GameCenter 指南(实现仿官
    ​​ 李华明Himi ​​​原创,转载务必在明显处注明    这里Himi给出对于开发iOS的朋友们整理一个指南集合,其中主要包括申请IDP需要注意的地方、有了开发者证书如......