当前位置:网站首页>Global type in description file
Global type in description file
2022-04-22 18:46:00 【Xiao Er Li】
This article will introduce TypeScript About the global type definition in the description file . This article will focus on JQuery As an example .
elicit questions
Directly in ts Use... In the document JQuery The syntax will prompt the following errors :

This is because JQuery.js There is no JQuery Type related notes , So it can't be ts The correct in the file is identified , At this time, you need to use the type annotation file , To get through ts and js Gap between , help ts To understand the JQuery Related to the grammar .
Define global variables
jquery.d.ts
// Define the receive parameter as a function , The return value is void Global variable of
declare var $: (param: () => void) => void;
At this time ts It can be correctly identified in the document $ Represents the type of .
demo.ts
$(function () {
alert(123)
})
Define global functions
The first way
jquery.d.ts
// Define global functions : The function name is $, Parameter type is function , The return value is void
declare function $(param: () => void): void;
At this time, you can ts Identify the defined global function in the file .
demo.ts
$(function () {
alert(123)
})
The second way
jquery.d.ts
// Define global functions : The function name is $, Parameter type is string, The inside contains a file named html, receive string Parameters , A function whose return value is an object
declare function $(param: string): {
html: (html: string) => {}
}
At this time, you can ts Identify the defined global function in the file .

The third way
It is a variant of the second way , Make the code readable .
jquery.d.ts
// Define global functions
interface JqueryInstance {
html: (html: string) => {};
}
declare function $(selector: string): JqueryInstance
At this time, you can ts Identify the defined global function in the file .
demo.ts
$(function () {
$('body').html('<div>123</div>')
})
The fourth way
It is a variant of the third way , Conducive to code management , The encapsulation idea in design pattern is used .
jquery.d.ts
// Define global functions
interface JqueryInstance {
html: (html: string) => JqueryInstance;
}
// Use interface The grammar of , Implement function overloading
interface JQuery {
(readyFunc: () => void): void
(selector: string): JqueryInstance
}
declare var $:JQuery
At this time, you can ts Identify the defined global function in the file .
demo.ts
$(function () {
$('body').html('<div>123</div>')
})
Define the namespace
jquery.d.ts
// How to type an object , And type definition of classes , And the nesting of namespaces
declare namespace $ {
namespace fn {
class init {}
}
}
At this time, you can ts The defined namespace is used in .
demo.ts
$(function () {
new $.fn.init()
})
summary
The function of type definition file :
help ts To understand the introduction of js What's in the library , because js There is no... In the library ts The concept of type , So in ts Introduction in js When I was in the library , Can't understand ’$' What is the , So you need to define type annotations , help ts To understand the .
Global function overload : You can use the same function name , But functions with different parameter types and return value types .
版权声明
本文为[Xiao Er Li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221841308702.html
边栏推荐
- 花样机模板电脑CAD免费打板转格式软件PS300B使用步骤教程:日本兄弟牌Brother花样机通用CAD画图打板.dxf文件转.emb文件
- 模块代码的类型描述文件
- Zeromq: Event Monitoring
- 对比两个list的差异,list的每个元素是string
- 安全与机会的平衡---st股投资标的选择与思考
- B03 based on STM32 single chip microcomputer independent key control nixie tube stopwatch Proteus design, Keil program, C language, source code, standard library version
- WiFi技术概述:WiFi那些事
- 在 Kubernetes 集群中部署现代应用的通用模式
- C# 从list 或者string中随机获取一个
- 微星小飞机 性能监控设置
猜你喜欢

文本分类问题中的一些指标

Golang gin websocket problem

不产桩不建桩,充电服务商NaaS靠什么崛起?

The sandbox has entered into cooperative relations with slipknot and knotfest to jointly build knotverse

General mode of deploying modern applications in kubernetes cluster

CDATA drivers for JIRA complete features and attributes

Model Inspector — 软件模型静态规范检查工具

leetcode - 234. 回文链表

【论文泛读】Easing Embedding Learning by Comprehensive Transcription of Heterogeneous Information Networks

C language precompiling (preprocessing) define
随机推荐
Interpretation of the seven key tasks of the guiding opinions on comprehensively improving the development level of digital economy in Jiangsu
062 反序列化漏洞
TypeScript中的命名空间使用
Model Inspector — 软件模型静态规范检查工具
企业沟通软件与其他通信对比的优势有哪些
花样机模板电脑CAD免费打板转格式软件PS300B使用步骤教程:日本兄弟牌Brother花样机通用CAD画图打板.dxf文件转.emb文件
山东大学项目实训树莓派提升计划二期(九)继承和多态
【接口测试基础】第十一篇 | 详解Postman关联接口及批量执行用例集
力扣-931.下降路径最小和
【Proteus仿真】51单片机制作简易计算器+ LCD1602显示
浅析局域网聊天软件的能力
不产桩不建桩,充电服务商NaaS靠什么崛起?
Proteus 8.9SP2仿真软件
Recommendation of safe, fast and low-cost futures companies in 2022?
C语言的预编译(预处理)#define
对比两个list的差异,list的每个元素是string
如何做好性能压测(一):压测环境的设计和搭建
CDATA drivers for JIRA complete features and attributes
Why don't I use flomo anymore
2022語言與智能技術競賽再昇級,推出NLP四大前沿任務