当前位置:网站首页>Typescript TS basic knowledge type declaration
Typescript TS basic knowledge type declaration
2022-07-07 21:45:00 【Tangtang 246】
Define variable types 、 Function parameter type 、 Function return value type
let Variable : type
let Variable : type = value
function fn( Parameters : type , Parameters : type ): return type { ...... }
1. : number Numbers
2. : string character string
3. :boolean Boolean value
4. : Literal The value of the limiting variable is the literal value ,eg:let a: 10; a = 11 Will report a mistake
5. : any Any type , It is equivalent to turning off... For this variable TS Type detection , Not recommended
let e
e = 'hello' // Sure
let s: string
s = e // Sure 6. : unknown Type safe any, Cannot be assigned directly to other variables
let e:unknown
e = true // Sure
e = 'hello' // Sure
let s: string
s = e // Report errors
// solve
if(typeof e === 'string'){ s = e }
s = e as string // Types of assertions , To tell the parser the actual type of the variable s = <string>e7. : void Null value No value ( or undefined) It is often used to set the return value of a function
8. : never Cannot be any value , Never return results (throw new Error(' Wrong report '))
9. : object arbitrarily js object , This form is not commonly used , The following methods are often used
// {} Used to specify which attributes are included in the object , grammar :{ Property name : Property value type , Property name : Property value type ,... }
// Add... After the attribute name ?, Indicates that the property is optional
let b: {name: string, age?: number}
b = {name: 'tom', age: 18} // Sure
b = {name: 'jack'} // Sure
// [propName: string]: any Represents any type of property
let c: {name: string, [propName: string]: any}
c = {name: 'may', age: 12, gender: ' Woman '} // Sure
// Set the type declaration of the function structure , grammar :( Shape parameter : type , Shape parameter : type ,...) => return type
let d: (a:number, b:number) => number
d = function(n1:string, n2: string): number{ return 10 } // Report errors , Wrong parameter type 10. : array arbitrarily js Array , Commonly used way : type [],Array< type >
let e: string[]
let f: Array<number>11. : tuple Tuples ,TS New type , Fixed length array , grammar :[ type , type , type ,... ]
let h: [string, string]
h = ['hello', 123] // Report errors , Type error
h = ['hello', 'world', ' Hello '] // Report errors , There are too many 12. : enum enumeration ,TS New type , List the possible situations
enum Gender{
Male = 0,
Female = 1
}
let i: {name: string, gender: Gender}
i = {
name: 'hi',
gender: Gender.Male, // In the database gender Save value as 0
}
console.log(i.gender === Gender.Male)notes :
a. If the declaration and assignment of variables are done at the same time ,TS You can automatically type detect variables , Type declarations can be omitted
b. have access to | ( or ) To link multiple types ( Joint type ), Combined with literal , Variables can be limited between certain values
c. Variables are declared without specifying a type , be TS The parser automatically determines that the variable type is any, If you don't know the type, you can use unknown type
d. & Indicates simultaneous ,let j: {name: string} & {age: number},j At the same time, it meets the requirements of name and age attribute
e. Type the alias type
type myType = 1|2|3|4|5
let k: myType
let l: myType边栏推荐
- Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
- Using enumeration to realize English to braille
- L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
- Qt编写物联网管理平台39-报警联动
- [UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]
- Jenkins user rights management
- Codeforces 474 F. Ant colony
- The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
- Google SEO external chain backlinks research tool recommendation
- Restore backup data on persistent volumes
猜你喜欢

Validutil, "Rethinking the setting of semi supervised learning on graphs"
SQL injection error report injection function graphic explanation
![[开源] .Net ORM 访问 Firebird 数据库](/img/a2/4eff4f0af53bf3b9839a73019a212f.png)
[开源] .Net ORM 访问 Firebird 数据库

解决uni-app中uni.request发送POST请求没有反应。

Reptile combat (VII): pictures of the king of reptiles' heroes
MySQL storage expression error

Win11游戏模式怎么开启?Win11开启游戏模式的方法

Dry goods sharing | devaxpress v22.1 original help document download collection

大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?

Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
随机推荐
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
Description of the difference between character varying and character in PostgreSQL database
Talk about relational database and serverless
Win11如何解禁键盘?Win11解禁键盘的方法
The difference between NPM uninstall and RM direct deletion
FatMouse&#39; Trade(杭电1009)
特征生成
Mahout-Pearson correlation的实现
Solve the problem of uni in uni app Request sent a post request without response.
Deployment, recall and deletion solutions - stsadm and PowerShell "suggestions collection"
UVA 12230 – crossing rivers (probability) "suggested collection"
HOJ 2245 浮游三角胞(数学啊 )
An overview of the latest research progress of "efficient deep segmentation of labels" at Shanghai Jiaotong University, which comprehensively expounds the deep segmentation methods of unsupervised, ro
An in-depth understanding of fp/fn/precision/recall
Ad domain group policy management
GridView defines its own time for typesetting "suggestions collection"
Jetty: configure connector [easy to understand]
Word inversion implements "suggestions collection"
解决uni-app中uni.request发送POST请求没有反应。
ISO 26262 - considerations other than requirements based testing