In the last section, we briefly introduced the concept, functions and advantages and disadvantages of Ts, so this section will formally enter the introduction of Ts: data types.

The data type

One of the highlights of Ts is strongly typed variables, which must be declared at the same time as their type. The syntax for a type declaration is: + data type, e.g. String.

Ts and Js share the following data types

  • boolean
  • string
  • number
  • undefined
  • null

Boolean type

Boolean value type, with only two values, true or false, essential for business logic judgment

let success: boolean = true
let error: boolean = false
Copy the code

Type string

String type, a character enclosed by single parentheses or double parentheses or a string template

let singleStr: string = 'Ts string'
let doubleStr: string = "Ts string"
let templeteStr: string = `Ts string`
Copy the code

The number type

Numeric type, including integer, decimal, binary, octal, and so on

let num: number = 1
let floatNum: number = 1.1
let nan: number = NaN
Copy the code

One important thing to note about numeric types is NaN(Not a number), which, for some reason, is also a numeric type in Ts and will compile without error. And then there’s Infinity plus Infinity minus Infinity

Undefined type

When you declare a variable but do not assign a value to it, the type of the variable is undefined

let u: undefined = undefined
let a: number
console.log(a)
Copy the code

When ts is compiled into js, undefined will be printed

Null type

Similar to the use of undefined, which represents null, null means that a reference is null

let n: null = null
Copy the code

Void, enum, any, array, tuple, etc

Void type

Void void void void void void void void void void void void void void void void

function sayHello (): void {
    console.log('hello Ts')}Copy the code

Void void differs from undefined and null in that undefined and null are subtypes of other types and can be assigned to other types, such as numeric types

// No error will be reportedlet num: number = undefined
Copy the code

Assigning a void value to number causes an error

let v: void
letNum: number = v // Error // Type'void' is not assignable to type 'number'.
Copy the code

Enum type

Enumeration type. So, for example, the gender of a person, except for male, is female, oh, there are indescribable, but those are fixed enumerable, and one year is fixed 12 months, and those can be represented by enumerated types

Shemale enum People {male, female, Console. log(people.male) // You can also assign values to enumeration enum People {male:'male',
    female: 'woman',
    shemale: 'love'
}
console.log(People.male) // 'male'
Copy the code

Any type

This is an amazing type, and you can probably guess by looking at any, that it’s an arbitrary type. Front-end ER all know, JS is a very flexible language, data types can be constantly changing, writing accustomed to JS front-end ER, it is difficult to define a good variable at the beginning of the type, at this time can use any type to define

let a: any = 123
a = 'Wooden man'
a = false
Copy the code

If a variable is declared without a type, it is identified as any

// This is equivalent to the abovelet a
a = 123
a = 'Wooden man'
a = false
Copy the code

Any is not recommended, though it may seem “inclusive”, because the purpose of using Ts is to regulate development. If you overuse any, it doesn’t make much sense. Use any as a transition from JS to Ts.

conclusion

I like to analyze big things by gradually breaking them down into small and fine ones, advancing layer by layer. Now is also the era of fast consumption, how to use spare time to improve, is also a skill to learn.