当前位置:网站首页>Built in objects for typescript
Built in objects for typescript
2022-06-09 04:47:00 【Large American without sugar】
JavaScript There are many built-in objects in , They can be directly in TypeScript As a defined type .
The built-in object refers to the object in the global scope according to the standard (Global) Objects that exist on . The standard here refers to ECMAScript And other environments ( such as DOM) Standards for .
ECMAScript Built in objects for
ECMAScript The built-in objects provided by the standard are :
Boolean、Error、Date、RegExp etc. .
We can do it in TypeScript Define variables as these types :
let b: Boolean = new Boolean(1);
let e: Error = new Error('Error occurred');
let d: Date = new Date();
let r: RegExp = /[a-z]/l
More built-in objects , You can see MDN Documents .
And their definition file , It's in TypeScript In the definition file of the core library .
DOM and BOM Built in objects for
DOM and BOM The built-in objects provided are :
Document、HTMLElement、Event、NodeList etc. .
TypeScript These types are often used in :
let body: HTMLElement = document.body;
let allDiv: NodeList = document.querySelectorAll('div');
document.addEventListener('click', funciton(e: MouseEvent) {
// ...
});
Their definition files are also in TypeScript In the definition file of the core library .
TypeScript Definition file of core library
TypeScript The definition file of the core library defines the types required by all browser environments , And it is preset in TypeScript Medium .
When you are using some common methods ,TypeScript In fact, I've done a lot of types of judgment for you , such as :
Math.pow(10, '2'); // error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
In the example above ,Math.pow Must accept two number Parameters of type . in fact Math.pow The types of are defined as follows :
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;
}
For another DOM Examples in :
document.addEventListener('clicl', function(e) {
console.log(e.targetCurrent); // error TS2339: Property 'targetCurrent' does not exist on type 'MouseEvent'.
});
In the example above ,addEventListener The method is in TypeScript Defined in the core library :
interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEvent {
addEventListener(type: string, listener: (ev: MouseEvent) => any, useCapture?: boolean): void;
}
therefore e To be inferred as MouseEvent, and MouseEvent It's not targetCurrent Attribute , That's why I made a mistake .
use TypeScript Write Node.js
Be careful ,TypeScript The definition of the core library does not contain Node.js part .
Node.js Not part of the built-in object , If you want to use TypeScript Write Node.js, A third-party declaration file needs to be introduced :
npm install @types/node --save-dev
Extract from :《TypeScript Introductory tutorial 》
边栏推荐
- API Gateway Apache apisix Installation and Performance Test on AWS graviton3
- P5354 [Ynoi2017] 由乃的 OJ(树剖、位运算)
- proteus仿真Arduino
- The half year revenue of mushroom street was 168million yuan: a year-on-year decrease of 29% and an operating loss of 240million yuan
- Camtasia studio2022 activation code serial number
- My creation anniversary
- The latest JMeter pressure test in the whole network is not much to say. I just want to teach you to use JMeter to write script pressure test as soon as possible
- PHP e-signature SaaS API docking process
- 2022 safety officer-c certificate examination practice questions simulated examination platform operation
- Brain: an interpretable deep learning framework for Alzheimer's disease (AD) classification
猜你喜欢
![[sword finger offer II 001. integer division] the same as leedcode 29 Divide two numbers](/img/cb/83491f2ee6f99c2be44db814b4b211.jpg)
[sword finger offer II 001. integer division] the same as leedcode 29 Divide two numbers

Debugging -- debugging objects and events

openGL_ 05 simple application of shader

National information security competition for college students (ciscn) -reverse- recurrence (part)

Win10 registry failed to save changes to permissions access denied

Win10注册表无法保存对权限所作的更改拒绝访问

The latest JMeter pressure test in the whole network is not much to say. I just want to teach you to use JMeter to write script pressure test as soon as possible

Mmdet modify the font size, position, color and fill box of the detection box

150 basic acoustic knowledge that acoustic engineers should know (full text)

keepalived配置虚拟IP
随机推荐
openGL_01-创建窗口
Proteus simulation Arduino
Lua operator
My creation anniversary
StepN分析
数据库连接问题,换版本后无法获取连接
Openstack Learning Series 12: installing CEPH and docking openstack
[SWPU2019]ReverseMe
Hengyuan cloud (gpushare)_ Beyond the model of pre training NLP
Gradle channel package configuration
The half year revenue of mushroom street was 168million yuan: a year-on-year decrease of 29% and an operating loss of 240million yuan
P5321 [BJOI2019]送别(LCT)
Database connection problem, unable to obtain connection after version change
2022年安全员-A证考试试题及在线模拟考试
[sword finger offer II 001. integer division] the same as leedcode 29 Divide two numbers
(7) Attribute binding
P1779 小胡同学的跳板
Personalized brain connectome fingerprints: their importance in cognition
(6) Events
Wuqi_ New progress in vision language navigation: pre training and sim2real