typescript设置默认值_TypeScript 中神奇的 this 类型声明
创建了一个“重学TypeScript”的微信群,想加群的小伙伴,加我微信"semlinker",备注重学TS哟
从 TypeScript 2.0 开始,在函数和方法中我们可以声明 this
的类型,实际使用起来也很简单,比如:
function sayHello(this: void) {// this: void:表示在函数体内不允许使用this}
在上面的 sayHello 函数中,this
参数是伪参数,它位于函数参数列表的第一位。为什么说 this
参数是伪参数呢?因为以上的 sayHello 函数经过编译后,并不会生成实际的参数,该函数编译成 ES5 后的代码如下:
function sayHello() {// this: void:表示在函数体内不允许使用this}
那么在实际开发中,this
参数有什么用呢?下面我们来详细介绍一下 this
参数的一些应用场景。
一、未使用 this 参数
class Rectangle {private w: number;private h: number;
constructor(w: number, h: number) {this.w = w;this.h = h; }
getArea() {return () => {return this.w * this.h; }; }}
以上代码中,我们定义了一个 Rectangle
长方形类,该类中包含了两个私有的 w 和 h 属性,分别表示长方形的宽度和高度,此外还有一个 getArea
方法用于获取长方形的面积。在 getArea
方法中我们没有使用 this
参数,此时 this
的类型是 this,如下图所示:
二、使用 this 参数
class Rectangle {private w: number;private h: number;
constructor(w: number, h: number) {this.w = w;this.h = h; }
getArea(this: Rectangle) {return () => {return this.w * this.h; }; }}
与前面定义的 Rectangle
长方形类不同,在 getArea
方法中,我们使用了 this
参数,之后 this
的类型是 Rectangle
类型,如下图所示:
在 Rectangle
长方形类 getArea
方法中的 this
入参只是作为一个形式上的参数,供 TypeScript 做静态检查时使用,编译后并不会生成实际的入参。
三、禁止使用 this
有些时候,我们希望在方法中,禁止用户使用 this
。针对这种需求,你可以设置 this 参数的类型为 void
:
class Rectangle {private w: number;private h: number;
constructor(w: number, h: number) {this.w = w;this.h = h; }
getArea(this: void) {return () => {return this.w * this.h; }; }}
以上代码会提示以下异常:
Property 'w' does not exist on type 'void'.Property 'h' does not exist on type 'void'.
四、回调函数中 this
前端开发者日常经常需要跟回调函数打交道,比如在页面中监听用户的点击事件,然后执行对应的处理函数,具体示例如下:
const button = document.querySelector("button");// ?. -> TS 3.7引入的可选链button?.addEventListener("click", handleClick);
function handleClick() {console.log("Clicked!");// 'this' implicitly has type 'any' because it does not have a type annotation.this.removeEventListener("click", handleClick);}
对于以上代码,TypeScript 编译器会有以下错误提示:this
隐式具有 any
类型,这是因为它没有类型注解。为了解决这个问题,我们就可以显式指定 this
参数的类型:
const button = document.querySelector("button");button?.addEventListener("click", handleClick);
function handleClick(this: HTMLElement) {console.log("Clicked!");this.removeEventListener("click", handleClick);}
除此之外,TypeScript 2.0 还增加了一个新的编译选项:--noImplicitThis
,表示当 this 表达式值为 any 类型的时候,生成一个错误信息。
五、参考资源
- function-this-parameter
typescriptlang - typescript-2-0
▼往期精彩回顾▼在 TS 中如何实现类型保护?类型谓词了解一下在 TS 中你踩过默认值的坑么?给 ?? 运算符把坑填了!看到 TypeScript 泛型就头晕?给这是我开的方子!用上这几招,轻松实现 TS 类型提取TS 中的可辨识联合有啥用?请听我细细道来!是时候表演真正的技术了 - TS 分身之术
TypeScript never 类型
TypeScript 设计模式之适配器模式
在前端 Word 还能这样玩
typescript设置默认值_TypeScript 中神奇的 this 类型声明相关推荐
- typescript设置默认值_Typescript默认值以及可选参数
Typescript默认值以及可选参数 可选参数:在参数名后面,冒号前面添加一个问号,则表明该参数是可选的.如下代码:function buildName(firstName: string, las ...
- typescript设置默认值_TypeScript输入参数的默认值一例,以及对应生成的JavaScript代码分析...
const fun = ({ aa = 1, bb = 0 } = {}) => { console.log('aa: ' + aa); console.log('bb: ' + bb); re ...
- typescript设置默认值_TypeScript属性的默认值
我在TypeScript中定义了一个接口,如下所示: export interface User { id?: number; name?: string; logoUrl?: File; email ...
- sqlyog怎么设置默认值_mysql 中怎样设置默认值为系统日期
-- 方法一: 由于MySQL目前字段的默认值不支持函数,所以以create_time datetime default now() 的形式设置默认值是不可能的.代替的方案是使用TIMESTAMP类型 ...
- Vue组件传值-设置默认值
使用props接收值 1.default: 设置默认值 2.type:设定传值类型 3.require:设置必填项 代码示例: ·App.vue父组件调用子组件 <div class=" ...
- Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数
Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数 7.4.4 为外部参数设置默认值 开发者也可以对外部参数设置默认值.这时,调用的时候,也可以省略参数传递本文选自Swift ...
- PowerDesigner中如何给字符串字段设置默认值 .
参考:http://www.cnblogs.com/navy235/archive/2011/10/18/2216443.html 在PowerDesigner12.5中,给varchar或nvarc ...
- html5下拉列表默认值,element-ui中的select下拉列表设置默认值方法_简单_前端开发者...
element-ui中的select下拉列表如何设置默认值? 在element-ui的运用中,涉及到了select下拉列表.项目中需要将select的默认值给展示出来 那如何修改呢? 上element ...
- [转载] java给对象中的包装类设置默认值
参考链接: Java中的对象类Object 处理方法如下 主要适用于,对象中使用了包装类,但是不能给null需要有默认值的情况 /** * 处理对象中包装类,因为快捷签没有用包装类 * * @para ...
最新文章
- R语言plotly可视化:plotly可视化分组归一化直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部部边缘使用geom_rug函数添加边缘轴须图
- 谈钱太俗!难道开源软件只能讲道义?
- 利用excel办公软件快速拼凑sql语句
- 阿里异地多活与同城双活的架构演进
- animation of android (1)
- 训练日志 2018.10.7
- System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)...
- 教你在 Centos 7 中使用 DenyHosts防止ssh暴力破解(亲测)
- JSONObject.fromObject()
- 雷蛇鼠标垫真假区分_您真的需要鼠标垫吗?
- graphpad图片怎么导出矢量图_怎样将PS图像输出为矢量图
- Incorrect string value: '\xF0\x9F\x91\x93\xF0\x9F...' for column 'xxx' at row 1
- 基于药效团模型和分子动力学模拟对PTP-LAR抑制剂的研究
- python学习之人民币兑美元之间的转换
- CSS学习笔记 07、2D与3D转换
- protobuf报错:protoc did not exit cleanly. Review output for more information.
- 时序数据库与传统数据库的优势
- 新浪微博如何设置半年可见?需要会员权限吗?
- mysql pt监控_技术分享 | MySQL 监控利器之 Pt-Stalk
- java实现单链表就地逆置,单链表的就地逆置讲解