[TS study notes] variables and data types of typescript

The data type of ts, in order to make the written code more standardized and more conducive to management, type verification is added

1. String

You can use single quotes ' ' double quotes " " or backticks

let heroName:string = "安其拉"

2. Number type (number)

let age:Number =18;
age = 18.9
age =-17

3. Boolean

true / false cannot be replaced by 0, 1 like in js


let isSingleDog:boolean = true;
isSingleDog =false
isSingleDog =1 //报错 Type 'number' is not assignable to type 'boolean'.

Fourth, undefined and null unique values

These two types have no value other than themselves

let  undf:undefined = undefined;
let  nl:null = null;

5. Array type

There are two ways to define an array:
1. let Array name: type [] = [value 1, value 2]
You can add [] after the element type, indicating an array composed of elements of this type:

let list: number[] = [1, 2, 3];

2.let array name: Array<type> =[value 1, value 2]
The second way is to use array generics, Array<element type>:

let list: Array<number> = [1, 2, 3];

Features: type limit unlimited length

6. Tuple type (Tuple)

元组类型是数组的一种,表示一个已知元素数量和类型的数组,各元素的类型不必相同。 比如,你可以定义一对值分别为 string和number类型的元组。
let tup:[string,number,boolean]=['貂蝉',18,false]
tup=['吕布',20,true]

Access the element and length of the tuple

console.log(tup[0])
console.log(tup.length)

Features: 1. Specify the length of the tuple 2. Specify the type for each element

Seven, enumeration type

The enum type is a supplement to JavaScript's standard data types. Like other languages ​​such as C#, enumeration types can be used to give friendly names to a set of values.
Declaration syntax:

 enum 枚举名{
    
    
      枚举项1 =枚举值1
      枚举项2 =枚举值2
  }

Enumeration items generally use English or numbers.
Enumeration values ​​use integer numbers. The enumeration value can be defaulted, and 0, 1, 2, will be automatically added when compiling. . .

 //1创建枚举
 enum Gender{
    
    
     Boy  = 1,
     Girl = 2,
     Unknow=3
 }
 console.log(Gender.Boy) //1
 console.log(Gender.Girl)//2
 console.log(Gender.Unknow)//3
 //2使用枚举
 let userSex:Gender =Gender.Boy
 console.log(userSex)
 if(userSex == Gender.Boy){
    
    
     console.log('相等','是男的')
 }else{
    
    
     console.log('不是男的 ',userSex)
 }

If no value is assigned to the element, by default, the element number starts from 0, that is, the subscript

enum Gender2{
    
    
    Boy ,
    Girl ,
    Unknow
}
console.log(Gender2.Boy) //0
 console.log(Gender2.Girl) //1
 console.log(Gender2.Unknow)//2

8. Any type

any Any type, generally used when obtaining DOM objects. When receiving user input, or third-party code libraries, you can also use any

Nine, void type

No type, generally used in functions that do not return a value

Ten, never type


Never is the type of value that does not exist. It is generally used to throw an exception or an infinite loop.

Eleven, type inference

If the declaration and initialization of the variable are on the same line, the declaration of the variable type can be omitted

let hero='sting'
hero=18   //报错,因为hero的类型被推断为string类型

12. Joint type

Indicates that the value can be one of multiple types
Grammar declaration: let variable name: variable type 1 | variable type 2 = value

let dogName :string|null =prompt('请输入狗狗名称')
console.log('hello',dogName)

Guess you like

Origin blog.csdn.net/weixin_41884808/article/details/109219070