当前位置:网站首页>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>e
7. : 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
边栏推荐
- 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
- Wechat official account oauth2.0 authorizes login and displays user information
- Contour layout of margin
- The difference between NPM uninstall and RM direct deletion
- Lex & yacc of Pisa proxy SQL parsing
- Restore backup data on persistent volumes
- Cocos2d-x 游戏存档[通俗易懂]
- 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?
- Use br to back up tidb cluster data to azure blob storage
- Prometheus remote_ write InfluxDB,unable to parse authentication credentials,authorization failed
猜你喜欢
Goal: do not exclude yaml syntax. Try to get started quickly
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
Jenkins user rights management
Jerry's about TWS channel configuration [chapter]
The little money made by the program ape is a P!
Jerry's test box configuration channel [chapter]
【JDBC Part 1】概述、获取连接、CRUD
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
An in-depth understanding of fp/fn/precision/recall
随机推荐
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
Deadlock conditions and preventive treatment [easy to understand]
What stocks can a new account holder buy? Is the stock trading account safe
Meta force force meta universe system development fossage model
Automatic classification of defective photovoltaic module cells in electronic images
How to turn on win11 game mode? How to turn on game mode in win11
开户必须往账户里面赚钱吗,资金安全吗?
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
SQL injection error report injection function graphic explanation
Arlo's troubles
What is the reason for the abnormal flow consumption of 4G devices accessing the easygbs platform?
Using enumeration to realize English to braille
Demon daddy guide post - simple version
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
Debugging and handling the problem of jamming for about 30s during SSH login
Le capital - investissement est - il légal en Chine? C'est sûr?
Use br to recover backup data on azure blob storage
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade