Underlying data types

Boolean value

The most basic data types are simple true/false values, called Boolean in JavaScript and TypeScript (and in other languages as well).

Video Tutorial Address

let a: boolean = true
console.log(a)
let b: boolean = false
console.log(b)
let c: boolean
console.log(c)

Copy the code

The execution result

true
false
undefined

Copy the code

digital

Use number to indicate the number type. Like JavaScript, all numbers in TypeScript are floating point numbers. These floating point numbers are of type number. In addition to supporting decimal and hexadecimal literals, TypeScript also supports binary and octal literals introduced in ECMAScript 2015.

let a: number = 1
console.log(a)
let b: number = 0xf00d
console.log(b)
let c: number = 0b1010
console.log(c)

Copy the code

The execution result

1
61453
10

Copy the code

string

Use string to denote the string type. Like JavaScript, you can use double quotes (“) or single quotes (‘) to represent strings.

let a: string = 'a'
console.log(a)
let b: string = "b"
console.log(b)


Copy the code

The execution result

a
b

Copy the code

An array of

There are two ways to represent arrays in TS.

First, an element type can be followed by [] to represent an array of elements of that type

let a: number[] = [1, 2]
console.log(a)
let b: string[] = ["a", 'b']
console.log(b)


Copy the code

The results

[ 1, 2 ]
[ 'a', 'b' ]


Copy the code

Second, we use Array generics, Array< element type >

let a: Array<number> = [1, 2]console.log(a)let b: Array<string> = ['a', "b"]console.log(b)
Copy the code

The results

[ 1, 2 ][ 'a', 'b' ]
Copy the code

tuples

The tuple type allows you to represent an array with a known number and type of elements that need not be of the same type. For example, you can define a pair of tuples of type string and number.

let a: [string, boolean] = ["a", false]console.log(a)console.log(a[0])console.log(a[1])
Copy the code

The results

[ 'a', false ]afalse
Copy the code

The enumeration

enum Color {Red, Green, Blue}console.log(Color.Red)console.log(Color[0])enum lee {    A = "a",    B = "b"}console.log(lee.A)console.log(lee['A'])
Copy the code

The results

0Redaa
Copy the code

Enumeration does not specify a value and grows automatically from 0

any

let a: any = 1console.log(a)let b: any = 'b'console.log(b)
Copy the code

The results

1b
Copy the code

void

In a way, void is like the opposite of any; it means there is no type at all. When a function returns no value, you usually see the return type void:

const a = (): void => {    console.log('run a')}const b = () => {    console.log('run b')}b()a()
Copy the code

The results

run brun a
Copy the code