当前位置:网站首页>TS type declaration
TS type declaration
2022-07-05 23:33:00 【Wangyu】
Type declaration declare
1、 overview
1.1 declare What is it?
Premise : If there is a door to use now Typescript The library of writing , Want to be used by other developers , There are two ways
The way 1 pack ts Source file ( for ts The user to use ) And compiled js file ( for js The user to use )
The way 2 Provide compiled js Documents and supplies ts The type declaration used by the user
The advantages of the latter method : Small file size , It is very clear that the import declaration , And omit ts Time for further compilationType declaration definition :
Everything is not always going well , Code does not always have static types
The extended name of the type declaration file is .d.ts, It is untyped js Code attach ts Type of a way
- If there is a corresponding js file , extension .d.ts, Otherwise use .ts Extension
- Type declarations can only contain types , No value
- have access to declare Keyword declaration js A value is defined in the code
- It can be understood as :“ by my troth , I wrote it js The code exports the value of this type ”
Take a chestnut :
import {Subscribe} from './subscribe'
export declare class Observe impelement Subscribabel{
public_isScalar:boolean = false
constructor (subscribe?:string){
this.subscribe = subscribe
}
subscribe(observe?:paetialObserver):Subscription{
// xxxxx
}
}
/* Use TSC compile ,tsc -d Observabel.ts will i Get the type declaration file Observabel.d.ts, You can see , Only the type is preserved , No value , There is no specific content of any function */
import {Subscribe} from './subscribe'
export declare class Observe impelement Subscribabel{
_isScalar:boolean
constructor (subscribe?:string);
subscribe(observe?:paetialObserver):Subscription
}
1.2 declare The role of
- ts Users are using other people's compiled ts Code ,TSC Will look for and js File corresponding .d.ts file , Give Way ts Know what types are involved in the project , And code type prompt , No need to recompile ts Code , Greatly reduce compilation time
- Define global types that can be used anywhere in the project , You can use without importing ( External parameter type declaration , It needs to be distinguished from variable declaration )
- The description passes npm Third party modules installed ( External parameter module declaration )
1.2.1 External parameter variable declaration
stay ts among , Want to not add var Keyword defining a global variable will trigger an alarm , The correct way is to first forward typescript Statement , There are some global variables process
declare let process :{
env : {
NODE_ENV :'production' | 'development'
}
}
process = {
env : {
NODE_ENV :'production'
}
}
1.2.2 External parameter module declaration
- Definition : Put regular type declarations into special syntax declare module among
declare module ' Module name '{
export type MyType = number
export default MyType
}
- When used import Import ' Module name ’ after ,ts Get the information provided by the external parameter module declaration
- If you just want to tell ts, I want to import this module , The specific type will be determined later . Now let's assume any, Then keep only the head , Omit the declaration
declare module '@mc/error-boundary'
// It's just that some security will be missing when using
eg: import {x} from from '@mc/error-boundary'
x //an
- The module declaration supports wildcard import , This allows you to declare the type for any import path that matches the specified pattern , The import path uses wildcards * To match
import b from './test.json'
b // object
边栏推荐
- 数学公式截图识别神器Mathpix无限使用教程
- Comparison of parameters between TVs tube and zener diode
- 11gR2 Database Services for " Policy" and " Administrator" Managed databases (file I
- Code farmers to improve productivity
- YML configuration, binding and injection, verification, unit of bean
- yate.conf
- Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
- Use of metadata in golang grpc
- 动态规划 之 打家劫舍
- Go language introduction detailed tutorial (I): go language in the era
猜你喜欢

芯源&立创EDA训练营——无刷电机驱动

Comparison of parameters between TVs tube and zener diode

Three. JS VR house viewing

Basic knowledge of database (interview)

3:第一章:认识JVM规范2:JVM规范,简介;

orgchart. JS organization chart, presenting structural data in an elegant way

TVS管和ESD管的技術指標和選型指南-嘉立創推薦

98. 验证二叉搜索树 ●●

Neural structured learning - Part 3: training with synthesized graphs

Use of metadata in golang grpc
随机推荐
Attacking technology Er - Automation
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
代码农民提高生产力
February 13, 2022 -5- maximum depth of binary tree
February 13, 2022-4-symmetric binary tree
LeetCode——Add Binary
Xinyuan & Lichuang EDA training camp - brushless motor drive
How to quickly understand complex businesses and systematically think about problems?
21.PWM应用编程
98. 验证二叉搜索树 ●●
无刷驱动设计——浅谈MOS驱动电路
芯源&立创EDA训练营——无刷电机驱动
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
Krypton Factor-紫书第七章暴力求解
regular expression
帶外和帶內的區別
698. 划分为k个相等的子集 ●●
The interface of grafana tool displays an error, incluxdb error
进击的技术er——自动化
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?