当前位置:网站首页>TypeScript类型声明文件(三)
TypeScript类型声明文件(三)
2022-06-12 17:58:00 【走出自闭的鸟儿】
TS类型声明文件
类型声明文件概述
项目中的TS最终都会编译成JS,那我们在使用外部的三方库时,如何使用TS语法呢。
类型声明文件:用来为已存在的JS库提供类型信息
TS文件类型
- .ts文件
- 既包含类型信息又可执行代码
- 可以被编译为js文件,然后执行代码
- 用途:编写程序代码的地方
- .d.ts文件
- 只包含类型信息的类型声明文件
- 不会生成js文件,仅用于提供类型信息
- 用途:为JS提供类型信息
如果要为JS库提供类型信息,就要用到.d.ts文件
使用已有的类型声明文件
使用内置API类型声明文件
windows系统按ctrl键点击内置API即可打开文件


使用第三方库的类型声明文件
- 下载库时自带类型声明文件
- 由DefinitelyTyped提供
在TypeScript官网搜索下载即可
创建自己的类型声明文件
项目内共享类
如果多个ts文件都用到同一类型,可以创建.d.ts文件提供该类型,实现类型共享
- 创建index.d.ts类型声明文件
- 创建需要共享的类型,使用export导出
- 使用时通过import导入,省略.d.ts
为已有的JS文件提供类型声明
在导入一个js文件时,TS会自动加载于js文件同名的.d.ts文件
使用declare关键字:用于类型声明,为js文件中已存在的变量声明类型,而不是创建一个新的变量
// .js文件
let count = 1
// .d.ts文件
declare let count: number
对于type、interface这些TS特有的,就可以省略declare;对于let、function等JS和TS都能使用的,应该使用declare关键字
边栏推荐
- Figma从入门到放弃
- ES7 does not use parent-child and nested relationships to implement one to many functions
- C#操作数据库增查业务数据值内容案例学校表
- EASYCODE template
- Byte flybook Human Resources Kit three sides
- General differences between SQL server versions released by Microsoft in different periods so far, for reference
- SqlServer常用语句及函数
- MySQL learning notes
- 《用户体验要素:以用户为中心的产品设计》笔记
- 重构--梳理并分解继承体系
猜你喜欢

极限编程--根源分析实践

Esp-idf adds its own components

WinForm, crystal report making

NixOS 22.05安装过程记录

Vant3 +ts packaged simple step advancer component

High-Speed Layout Guidelines 未完...
![[csp]202012-2 optimal threshold for period end forecast](/img/40/9b59bd692bcfe05d16614cc6d55d1f.png)
[csp]202012-2 optimal threshold for period end forecast

C operation database added business data value content case school table

Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl

Arm64 Stack backtrack
随机推荐
Authorization in Golang ProjectUseing Casbin
Risc-v ide mounriver studio v1.60 update point introduction
Variable of C #
[CSP]202012-2期末预测之最佳阈值
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
C#的变量
leetcode 647. 回文子串
118. Yanghui triangle (dynamic planning)
73. matrix zeroing (marking method)
Introduction of one object one code tracing system
TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
Applet and app are owned at the same time? A technical scheme with both
Arm64 stack backtracking
一种好用、易上手的小程序IDE
《用户体验要素:以用户为中心的产品设计》笔记
Error record: illegalstateexception: optional int parameter 'XXXX' is
Research results of low code platform
Reconstruction -- sort out and decompose the inheritance system
Use applet to quickly generate app in seven steps
Kali2022 installing Armitage