当前位置:网站首页>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
边栏推荐
- 【矩阵乘】【NOI 2012】【cogs963】随机数生成器
- 大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
- Which financial products will yield high returns in 2022?
- TCP/IP 协议栈
- Wechat official account oauth2.0 authorizes login and displays user information
- Jerry's key to initiate pairing [chapter]
- Arlo's troubles
- POJ 3140 contents division "suggestions collection"
- EasyCVR配置中心录像计划页面调整分辨率时的显示优化
- Jerry's fast pairing does not support canceling pairing [article]
猜你喜欢
How does win11 time display the day of the week? How does win11 display the day of the week today?
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
Talk about relational database and serverless
Solve the problem of uni in uni app Request sent a post request without response.
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
Jenkins user rights management
ISO 26262 - considerations other than requirements based testing
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
Default constraint and zero fill constraint of MySQL constraint
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
随机推荐
开户还得用身份证银行卡安全吗,我是小白不懂
Hoj 2245 planktonic triangle cell (Mathematics)
【矩阵乘】【NOI 2012】【cogs963】随机数生成器
【JDBC Part 1】概述、获取连接、CRUD
2022 how to evaluate and select low code development platforms?
Reinforcement learning - learning notes 8 | Q-learning
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
[matrix multiplication] [noi 2012] [cogs963] random number generator
Cocos2d-x 游戏存档[通俗易懂]
Restore backup data on persistent volumes
Google SEO external chain backlinks research tool recommendation
How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
Awk processing JSON processing
Win11游戏模式怎么开启?Win11开启游戏模式的方法
FatMouse&#39; Trade(杭电1009)
Codeforces 474 F. Ant colony
Jerry's manual matching method [chapter]
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?