当前位置:网站首页>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

原网站

版权声明
本文为[Tangtang 246]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071448196940.html