Typescript data type declaration method and precautions

JavaScript data types are:
string, number, boolean, array, object, null, undefined, symbol, bigInt

Typescript data types are:
string, number, boolean, array, object, null, undefined, symbol, any, Tuple (tuple), enum (enumeration), void, never

Compared with javascript, typescript adds data type declarations when writing, and is more rigorous when writing code. You can find some writing errors when writing, such as data type errors in data assignment, etc.

Typescript-specific data types

Typescript has more data types than javascript:
any, Tuple (tuple), enum (enumeration), void, never

any - - - If you are not sure about the data type, you can first write it as any type
Tuple (tuple) - - - Similar to an array, but after the data type is defined by the tuple, the implemented data needs to be written in
enum (enumeration) in the order of the defined data - - - Often used to list multiple optional values
​​void - - - Often used to declare that a function has no return value
never - - - Indicates the type of value that never exists

Typescript declaration variable format:
let variable name: data type = value

JavaScript variable declaration format:
let variable name = value

Compared with javascript, Typescript has more restrictions on ":data type", which can also be omitted. If not, the data type will be automatically inferred as "any" type

Typescript data type declaration format

Example:


// string
let username:string = '史迪仔'

// number
let num:number = 1

// boolean
let flag1:boolean = true
let flag2:boolean = false

// array
let arr1:Array<number> = [1, 2, 3]
let arr2:Array<string> = ['1', '2', '3']
let arr3:Array<any> = ['1', 2, '3', false]
let arr4:number[] = [4, 5, 6]
let arr5:string[] = ['4', '5', '6']
let arr6:any[] = [true, '5', 6]

// object
let obj:Object = {
    
    name: 'xiaoixiao'}

// null
let b:null = null

// undefined
let a:undefined = undefined

// symbol
let sym:symbol = Symbol(1)

// any
let x:any = 'lalala'

// Tuple(元组)
let tuple:[string, number] = ['have a nice day', 888]

// enum(枚举)
enum Colors {
    
    
  green,
  red,
  blue
}
enum Colors2 {
    
    
  green = 'a',
  red = 6,
  blue
}

// void
let c:void = null
let d:void = undefined

// never
let e:never

// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
    
    
  throw new Error(message);
}

Typescript data type assignment considerations

1. The any type is the largest and can contain all other types, and other types of data can be assigned to the any type

2. void is usually the same as declaring that the function has no return value
. For example:

function fn(): void {
    
    
  console.log(1);
}

When void is used to declare a variable, only null and undefined can be assigned to it

3. By default, null and undefined are subtypes of all types. That is to say, you can assign null and undefined to variables of type number.

4. A tuple is an array with the type order set, and the data needs to be filled in the order of the declared type data

Otherwise, an error will be reported as follows:
insert image description here
5. The enum enumeration can have a value or not be written. If it is not written, it will be assigned from 0 to the next by default, 0, 1, 2, 3...
If the value is assigned to the number value type, it can be reversed To read the key name according to the value;
if the value is a string type, you cannot read the key name through the value

as the picture shows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_39111074/article/details/131983099