当前位置:网站首页>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边栏推荐
- Deadlock conditions and preventive treatment [easy to understand]
- Arlo's troubles
- Demon daddy C
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- Demon daddy guide post - simple version
- 201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
- Jerry's power on automatic pairing [chapter]
- Awk processing JSON processing
- How does win11 time display the day of the week? How does win11 display the day of the week today?
- TCP/IP 协议栈
猜你喜欢

L2: current situation, prospects and pain points of ZK Rollup

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

The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function

TCP/IP 协议栈
![[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)](/img/1e/055df228853d9b464fc4bcbde0a7ee.png)
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)

Validutil, "Rethinking the setting of semi supervised learning on graphs"

Google SEO external chain backlinks research tool recommendation
Talk about relational database and serverless

Debugging and handling the problem of jamming for about 30s during SSH login

Magic weapon - sensitive file discovery tool
随机推荐
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
L2:ZK-Rollup的现状,前景和痛点
Restapi version control strategy [eolink translation]
Virtual machine network configuration in VMWare
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
UVA 11080 – place the guards
Reinforcement learning - learning notes 8 | Q-learning
How does win11 time display the day of the week? How does win11 display the day of the week today?
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Codeforces 474 F. Ant colony
【colmap】稀疏重建转为MVSNet格式输入
How polardb-x does distributed database hotspot analysis
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
2022年在启牛开中银股票的账户安全吗?
开户还得用身份证银行卡安全吗,我是小白不懂
Unity3d 4.3.4f1 execution project
An in-depth understanding of fp/fn/precision/recall
QT compile IOT management platform 39 alarm linkage