当前位置:网站首页>TypeScript 的内置对象
TypeScript 的内置对象
2022-06-09 04:34:00 【大杯美式不加糖】
JavaScript 中有很多内置对象,它们可以直接在 TypeScript 中当做定义好了的类型。
内置对象是指根据标准在全局作用域(Global)上存在的对象。这里的标准是指 ECMAScript 和其他环境(比如 DOM)的标准。
ECMAScript 的内置对象
ECMAScript 标准提供的内置对象有:
Boolean、Error、Date、RegExp 等。
我们可以在 TypeScript 中将变量定义为这些类型:
let b: Boolean = new Boolean(1);
let e: Error = new Error('Error occurred');
let d: Date = new Date();
let r: RegExp = /[a-z]/l
更多的内置对象,可以查看 MDN 的文档。
而他们的定义文件,则在 TypeScript 核心库的定义文件中。
DOM 和 BOM 的内置对象
DOM 和 BOM 提供的内置对象有:
Document、HTMLElement、Event、NodeList 等。
TypeScript 中会经常用到这些类型:
let body: HTMLElement = document.body;
let allDiv: NodeList = document.querySelectorAll('div');
document.addEventListener('click', funciton(e: MouseEvent) {
// ...
});
它们的定义文件同样在 TypeScript 核心库的定义文件中。
TypeScript 核心库的定义文件
TypeScript 核心库的定义文件中定义了所有浏览器环境需要用到的类型,并且是预置在 TypeScript 中的。
当你在使用一些常用的方法的时候,TypeScript 实际上已经帮你做了很多类型判断的工作了,比如:
Math.pow(10, '2'); // error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
上面的例子中,Math.pow 必须接受两个 number 类型的参数。事实上 Math.pow 的类型定义如下:
interface Math {
/** * Returns the value of a base expression taken to a specified power. * @param x The base value of the expression. * @param y The exponent value of the expression. */
pow(x: number, y: number): number;
}
再举一个 DOM 中的例子:
document.addEventListener('clicl', function(e) {
console.log(e.targetCurrent); // error TS2339: Property 'targetCurrent' does not exist on type 'MouseEvent'.
});
上面的例子中,addEventListener 方法是在 TypeScript 核心库中定义的:
interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEvent {
addEventListener(type: string, listener: (ev: MouseEvent) => any, useCapture?: boolean): void;
}
所以 e 被推断成了 MouseEvent,而 MouseEvent 是没有 targetCurrent 属性的,所以报错了。
用 TypeScript 写 Node.js
注意,TypeScript 核心库的定义中不包含 Node.js 部分。
Node.js 不是内置对象的一部分,如果想用 TypeScript 写 Node.js,则需要引入第三方声明文件:
npm install @types/node --save-dev
边栏推荐
- hisi3559av100,MIPI相机输入接口调试
- Differences and principles between hashrouter and historyrouter
- 【6.824分布式系统】LEC 6&7: Fault Tolerance: Raft
- (8)样式绑定
- P5321 [bjoi2019] farewell (LCT)
- [w806 drummer's notes]fpu performance simple test - May 23, 2022
- openGL_ 01 create window
- 微信小程序:(异常)Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 解决方案和分析流程(这里一定有你要的答案)
- Number precision-- use / instance
- Internet behavior networking
猜你喜欢

2022年安全员-A证考试试题及在线模拟考试

声学工程师应知道的150个声学基础知识(全篇)

(3) Data binding instructions

golang---redis操作

迪文圆形智能屏上市

Harbor container installation and related feature deployment and use (SSL certificate +ad domain)

我的创作纪念日

Simulated 100 questions and answers of high voltage electrician examination in 2022

golang---各个类型变量的比较运算

golang---并发Goroutine
随机推荐
(2) V-cloak instruction
5、快速(分组)排序
Mmdet modify the font size, position, color and fill box of the detection box
keepalived配置虚拟IP
微信小程序:(异常)Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 解决方案和分析流程(这里一定有你要的答案)
Wuqi_ New progress in vision language navigation: pre training and sim2real
2022 safety officer-a certificate examination questions and online simulation examination
Implementation of horizontal and vertical center of unknown width and height elements
PDQ环境变量
渲染管线----通俗易懂向面试官介绍
Template: constant coefficient homogeneous linear recurrence (linear algebra, polynomial)
View local public IP
Test website construction + penetration + audit Part II penetration test
Golang ---- storage of variables and their contents
PHP e-signature SaaS API docking process
Camtasia studio2022 activation code serial number
Dart: Foundation
[006] [ESP32开发笔记] 使用Flash下载工具烧录固件步骤
Proteus simulation Arduino
big.js、bignumber.js 和 decimal.js 的区别