当前位置:网站首页>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
边栏推荐
- Open source OA development platform: contract management user manual
- Meta force force meta universe system development fossage model
- 为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
- FatMouse&#39; Trade (Hangdian 1009)
- Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
- Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
- POJ 3140 Contestants Division「建议收藏」
- POJ 3140 contents division "suggestions collection"
- Codemail auto collation code of visual studio plug-in
- UVA 11080 – place the guards
猜你喜欢
TCP/IP 协议栈
South China x99 platform chicken blood tutorial
Jerry's about TWS pairing mode configuration [chapter]
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?
Wechat official account oauth2.0 authorizes login and displays user information
Google SEO external chain backlinks research tool recommendation
Use camunda to do workflow design and reject operations
Automatic classification of defective photovoltaic module cells in electronic images
Talk about relational database and serverless
MySQL storage expression error
随机推荐
How to turn on win11 game mode? How to turn on game mode in win11
建立自己的网站(18)
POJ 3140 contents division "suggestions collection"
EasyUI date control emptying value
2022 how to evaluate and select low code development platforms?
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Reptile combat (VII): pictures of the king of reptiles' heroes
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
Win11如何解禁键盘?Win11解禁键盘的方法
私募基金在中国合法吗?安全吗?
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?
What stocks can a new account holder buy? Is the stock trading account safe
FatMouse&#39; Trade(杭电1009)
SQL injection error report injection function graphic explanation
QT compile IOT management platform 39 alarm linkage
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
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