当前位置:网站首页>如何理解 new (...args: any[]) => any
如何理解 new (...args: any[]) => any
2022-08-01 21:54:00 【华为云】
如何理解下面这段代码里的 new 操作?
/** * Checks if the value is an instance of the specified object. */isInstance(object: any, targetTypeConstructor: new (...args: any[]) => any) { return targetTypeConstructor && typeof targetTypeConstructor ==="function" && object instanceof targetTypeConstructor;}我们逐步分解。
() => any
该函数没有输入参数,返回任意类型。
(…args: any[]) => any
…args: any[]使用的是Rest Parameters构造,该构造本质上表示可以提供any类型的任何数量的参数。因为存在数量未知的any参数,所以参数的类型是any的数组。
最后,把 new 关键字补上。
new (…args: any[]) => any
此处的new关键字指定可以将此函数视为类构造函数,并使用new关键字进行调用。
回到文章开头的函数:
该函数是一个可以接受返回类型any的任意数量的参数(类型为any的函数),并且可以用作带有new关键字的构造函数。
看一个该函数具体消费的例子:
function isInstance(object: any, targetTypeConstructor: new (...args: any[]) => any) { return targetTypeConstructor && typeof targetTypeConstructor ==="function" && object instanceof targetTypeConstructor;}class Jerry{ constructor(private name:string){ this.name = name; }}const jerry: Jerry = new Jerry('Jerry');console.log(isInstance(jerry, Jerry));输出:true

如果把 new 关键字去掉,反而会报错:
Argument of type ‘typeof Jerry’ is not assignable to parameter of type ‘(…args: any[]) => any’.
Type ‘typeof Jerry’ provides no match for the signature ‘(…args: any[]): any’.
边栏推荐
- 恒星的正方形问题
- ModuleNotFoundError: No module named ‘yaml‘
- WEB 渗透之端口协议
- shell programming conventions and variables
- Flink cluster construction
- ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
- 19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
- Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- (*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
猜你喜欢

19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform

作业8.1 孤儿进程与僵尸进程

scikit-learn no moudule named six

【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理

AI应用第一课:支付宝刷脸登录

Based on php hotel online reservation management system acquisition (php graduation project)

kubernetes CoreDNS全解析

MySQL related knowledge

Anacoda的用途

File operations of WEB penetration
随机推荐
ImportError: `save_weights` requires h5py.问题解决
feel so stupid
入门数据库Days4
小程序容器+自定义插件,可实现混合App快速开发
Based on php online music website management system acquisition (php graduation design)
如何防范 DAO 中的治理攻击?
Based on php hotel online reservation management system acquisition (php graduation project)
NFT的10种实际用途(NFT系统开发)
Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
Unity Shader 常规光照模型代码整理
工程建筑行业数据中台指标分析
Chapter 12, target recognition of digital image processing
Shell programming conditional statement
ModuleNotFoundError: No module named ‘yaml‘
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
shell programming conventions and variables
Port protocol for WEB penetration
基于php影视资讯网站管理系统获取(php毕业设计)
VGUgarbage collector(垃圾回收器)的实现原理