当前位置: 首页 > news >正文

嘉兴网站建设电话花蝴蝶 高清直播

嘉兴网站建设电话,花蝴蝶 高清直播,深圳市南山区住房和建设局网站官网,做网站多少钱 优帮云TS系列之typeof 这里写目录标题 前言回顾#xff1a;Js中typeof的用法1、typeof的返回类型有哪些 Ts 中的 typeof 运算符作为类型判断的工具什么是 typeof#xff1f;类型保护与条件类型复杂类型 总结 前言 今天总结一下typeof相关的知识。 typeof在Js中也是常用来判断变量…TS系列之typeof 这里写目录标题 前言回顾Js中typeof的用法1、typeof的返回类型有哪些 Ts 中的 typeof 运算符作为类型判断的工具什么是 typeof类型保护与条件类型复杂类型 总结 前言 今天总结一下typeof相关的知识。 typeof在Js中也是常用来判断变量类型的关键字那么在Ts中的作用又是什么下面一起来看看。 回顾Js中typeof的用法 1、typeof的返回类型有哪些 “undefined” typeof undefined“object” typeof null或者其他任意对象至于null为什么返回的类型是object可以参考这篇文章typeof null is ‘object’ “boolean” typeof true“number” typeof 1“bigint” typeof BigInt(1)“string” typeof “symbol” typeof Symbol(1)“function” typeof function a () {}Ts 中的 typeof 运算符作为类型判断的工具 什么是 typeof typeof 是 TypeScript 中的运算符用于获取给定表达式的类型。返回一个表达式类型的字符串。 一个简单的例子 const num 1; const str Hello; console.log(typeof num); // 输出 number console.log(typeof str); // 输出 string在这个例子中typeof num 返回 “number”而 typeof str 返回 “string”很明显这和js中没有什么区别。 类型保护与条件类型 typeof 运算符的真正强大之处在于它与条件类型的结合使用。通过将 typeof 与条件类型结合我们可以在运行时对不同类型的代码进行条件分支。 举个例子 比如我们想要实现一个函数将传入的数字或者字符串类型的数据自增1并返回 function test (num: number | string): number | string {return num } //这时候ts报错提示An arithmetic operand must be of type any, number, bigint or an enum type. //意思是算术操作数的类型必须是“any”、“number”、“bigint”或枚举类型这个时候typeof就可以派上用场改造如下 function test (num: number | string): number | string {if (typeof num number) {return num;} else {return num 1;} }复杂类型 typeof 判断复杂类型时和js中的用法一样object、“function”、“symbol” 等等。 接下来我们看一下实际在开发中用的场景 1、例如我们有个对象如果对象嵌套深或者对象属性较多时那么你就可以直接用tyoeof来获取这个对象的属性中的所有类型。 const appconfig {id: 1,text: app,appName: app,appImg: https://app,appType: app,positionNum: 1,url: https://app }type appType typeof appconfig type appType {id: string;text: string;appName: string;appImg: string;appType: string;positionNum: number;url: string; } const appconfigtest: appType {} //所以你就可以直接用appType来约束新定义对象appconfigtest2、也可以获取对象中单个属性的类型 type appType typeof appconfig.appImg //type appType string3、当我想要通过函数创建并获取类的实例那么就可以通过typeof获取到class构造函数的类型 class APP {name: string;img: string;constructor (name: string, img: string) {this.name name;this.img img;} } //(parameter) CP: new (name: string, img: string) APP function createApp(CP: typeof APP, name: string, img: string) {return new CP(name, img); }总结 到这里我们就基本了解了typeof在ts中常见的一些用法 其它用法还需继续探索分享学习。
http://www.zqtcl.cn/news/104525/

相关文章:

  • 小白一步步做网站开题报告旅游网站建设
  • 鞋帽箱包网站建设怎么给网站做外链邵连虎
  • linux网站建设模板上海发布公众号官网
  • 信息科技有限公司网站建设网站运营主要做什么
  • 广州建筑公司网站网站上的动态图怎么做
  • win10系统可以做网站搭建网站和微信同步建设
  • 在哪里能找到做网站的人医疗网站建设意见
  • 网站制作及实现wordpress在线工具
  • 网站制作中企动力优响应式网站建设有利于seo
  • 区块链媒体网站建设wordpress页脚内容居中
  • php手机网站开发工具成都的教育品牌网站建设
  • 苏州建网站要多少钱八爪鱼采集器 wordpress
  • 确定网站风格thinkphp相比Wordpress
  • 网站全屏代码wordpress无法连接ftp
  • 做ppt配图好用的网站重庆制作网站有哪些
  • 门户网站建设进度安卓手机开发者模式
  • 招商网站建设需要什么网站开发 在线数据库
  • 创建网站代码网站二级页怎么做
  • 网站建设 前沿文章建设网站网站建设公司
  • dede网站seo微信开店怎么注册开店流程
  • 苏华建设集团有限公司网站wordpress 普通文本 quot
  • 网站首页倒计时功能怎么做学网站开发技术
  • 上海网站备案流程欧宇公司网络建设方案
  • 网站营销型办公室装修费用会计分录
  • 个人网站网页设计模板学校ftp服务器做网站
  • 黄江网站建设外贸公司用的采购储运财务软件
  • 优化网站公司做网站建设
  • 门户网站的盈利模式网站建设中备案
  • 代码需求网站织梦怎么关闭网站
  • 浙江工信部网站备案查询东圃做网站