当前位置:网站首页>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
边栏推荐
- Krypton Factor-紫书第七章暴力求解
- asp. Net pop-up layer instance
- (4) UART application design and simulation verification 2 - RX module design (stateless machine)
- When to use useImperativeHandle, useLayoutEffect, and useDebugValue
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- The interface of grafana tool displays an error, incluxdb error
- Scala concurrent programming (II) akka
- There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
- 基于脉冲神经网络的物体检测
- 秒杀系统的设计与实现思路
猜你喜欢
Go语言实现原理——锁实现原理
Comparison of parameters between TVs tube and zener diode
进击的技术er——自动化
开关电源Buck电路CCM及DCM工作模式
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
Go language implementation principle -- map implementation principle
CIS基准测试工具kube-bench使用
Neural structured learning 4 antagonistic learning for image classification
两数之和、三数之和(排序+双指针)
随机推荐
Déterminer si un arbre binaire est un arbre binaire complet
White hat talks about web security after reading 2
UVA11294-Wedding(2-SAT)
2:第一章:认识JVM规范1:JVM简介;
Leetcode buys and sells stocks
芯源&立创EDA训练营——无刷电机驱动
regular expression
Three. Js-01 getting started
TVS管 与 稳压二极管参数对比
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
Krypton Factor-紫书第七章暴力求解
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
asp.net弹出层实例
Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
Objective C message dispatch mechanism
February 13, 2022 -5- maximum depth of binary tree
98. Verify the binary search tree ●●
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
yate. conf
698. 划分为k个相等的子集 ●●