当前位置:网站首页>Typescript - declaration files and built-in objects
Typescript - declaration files and built-in objects
2022-06-10 22:35:00 【Little sheep fighting bug】
Preface
This paper mainly records TypeScript Declaration files and built-in objects in , Summary of daily study .
One 、 Declaration file
When using third-party libraries , We need to refer to its declaration file , To get the corresponding code completion 、 Interface prompt and other functions .
️、 What is a declaration statement
If we want to use a third-party library jQuery, A common way is in the html Pass through <script> Tags introduced jQuery, Then you can use global variables $ perhaps jQuery 了 . But in ts in , The compiler doesn't know $ or jQuery What is it? . At this time , We need to use declare var To define its type .
declare var jQuery: (selector: string) => any;
jQuery('#foo');️、 What is a declaration document
Usually we put the statement in a separate file (jQuery.d.ts) in , This is the declaration file .
Declaration file must be in .d.ts For the suffix .
Generally speaking ,ts Will parse all the... In the project *.ts file , Of course, it also includes .d.ts Final document .
️、 Third party statement documents
Recommended @types Unified management of third-party library statement file [email protected] It's easy to use , Direct use npm Install the corresponding declaration module , With jQuery give an example :
npm install @types/jquery --save-devTwo 、 Built-in objects
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 :Boolean、Number、String、Error、Date、RegExp etc. .
let b: Boolean = new Boolean(1)
let n: Number = new Number(true)
let s: String = new String('abc')
let d: Date = new Date()
let r: RegExp = /^1/
let e: Error = new Error('error message')
// let bb: boolean = new Boolean(2) // Report errors : You can't type “Boolean” Assign to type “boolean”.“boolean” It's primitives , but “Boolean” Is a wrapper object .
️、DOM and BOM Built in objects for :Window、Document、 HTMLElement、DocumentFragment、Event、NodeList etc. .
const div: HTMLElement = document.getElementById('app')
const divs: NodeList = document.querySelectorAll('div')
document.addEventListener('click', (event: MouseEvent) => {
console.dir(event.target)
})
const fragment: DocumentFragment = document.createDocumentFragment()️、TypeScript Definition file of core library Defines the types needed 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') // Report errors : type “string” Argument to cannot be assigned to type “number” Parameters of .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('click', function(e) {
// console.log(e.targetCurrent); // Report errors : type “MouseEvent” Property does not exist on “targetCurrent”.
});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
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边栏推荐
- How to stimulate the vitality and driving force of cultural innovation
- [tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction
- Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
- A number that appears only once in an array
- 在D天内送达包裹的能力[抽象类二分--抽象判定方式]
- 很流行的状态管理库 MobX 是怎么回事?
- 小微企业如何低成本搭建微官网
- Whale conference empowers intelligent epidemic prevention
- 自己搞了一个相亲软件的源码,用兴趣的可以聊聊
- Management solution for whale conference smart scenic spot
猜你喜欢
![[applet] vant sliding cell adds the function of clicking other positions to close automatically](/img/ef/3d954323a981026e2b917428cbc92b.png)
[applet] vant sliding cell adds the function of clicking other positions to close automatically

Sealem Finance打造Web3去中心化金融平台基础设施

【小程序】Vant滑动单元格添加点击其他位置自动关闭的功能

【TcaplusDB知识库】TcaplusDB查看进程状态介绍

【TcaplusDB知识库】TcaplusDB日常巡检介绍

鲸会务智慧景区管理解决方案
![在D天内送达包裹的能力[抽象类二分--抽象判定方式]](/img/16/026c017e1c54d580fa2d237aa4605c.png)
在D天内送达包裹的能力[抽象类二分--抽象判定方式]

很流行的状态管理库 MobX 是怎么回事?

Tcapulusdb Jun · industry news collection (VI)

Model construction of mmdetection
随机推荐
【小程序】Vant-Weapp Radio单选框组件无法触发bind:Change事件
Add, delete, query and modify [MySQL] table data (DML)
Implementation of simply untyped lambda calculus
Model construction of mmdetection
SQL exercise 4: string processing function
CCF CSP 202109-1数组推导
Tcapulusdb Jun · industry news collection (III)
Simply Untyped Lambda Calculus的实现
Back to table query of MySQL? How to avoid it?
笔记(五)- JVM
【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍
鲸会务为智能化防疫工作赋能
Web3生态去中心化金融平台——Sealem Finance
TcaplusDB君 · 行业新闻汇编(四)
C语言内功修炼【整型在内存中的存储】
Shell basic concepts
《暗黑破坏神不朽》数据库资料站地址 暗黑不朽资料库网址
CCF CSP 202109-3 脉冲神经网络
SQL server queries are case sensitive
Array plus one