- Boolean Boolean
let isDone: boolean = true
Copy the code
- The number of digital
- Like JS all numbers are floating point numbers supporting decimal, hexadecimal, binary, octal
let decLiteral: number = 6
let hexLiteral: number = 0xf00d
let binaryLiteral: number = 0b1010
let octalLiteral: number = 0o744
Copy the code
- String string
- You can use double or single quotes to represent strings
let str: string = 'string'
Copy the code
- You can also use template strings
let nameStr: string = 'bob'
let age: number = 18
let sentence: string = ` Hello my name is ${nameStr}
I'll be ${age + 1} years old next month`
Copy the code
- Array an Array
- The element type is followed by [], indicating that the elements of the type form an array
let listNum: number[] = [1.2.3]
Copy the code
- Use array generics
let listStr: Array<string> = ['1'.'2'.'3']
Copy the code
- The Tuple tuples
- Represents an array with a known number and type of elements, not necessarily the same type
// You can define a pair of tuples of type string and number
let x: [string.number] = ['A'.1]
Copy the code
- Enum enumeration
- Enumeration types are used to give friendly names to a set of values
enum Color {Red,Green,Blue}
let c: Color = Color.Green
Copy the code
- By default, elements are numbered from 0. You can also manually specify the values of the members.
// For example, we would change the above example to number from 1
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
// Alternatively, all manual assignments are used:
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
Copy the code
- One of the conveniences provided by an enumerated type is that you can refer to its name by enumeration. For example, if we know that the value is 2, but are not sure which name it maps to in Color, we can look up the corresponding name
enum Color { Red = 1, Green, Blue }
console.log(Color[2]) // Green
Copy the code
- Any specifies a type for variables of unknown type
- We don’t want the type checker to check these values and just pass them through compile-time checks. Then we can mark these variables with type any
let notSure: any = 4
notSure = 'matbe a string instead'
notSure = true
Copy the code
- The any type is also useful when you only know part of the data type. For example, you have an array that contains different types of data
let list: any[] = [1.true.'free']
list[1] = 100
Copy the code
- Void means there is no type
- A return type of void is commonly seen when a function returns no value
function warnUser() :void {
console.log(1)}Copy the code
- Declaring a variable of type vold can only give it undefined
let unusable: void = undefined
Copy the code
- Null, and Undefined
let u: undefined = undefined
let n: null = null
Copy the code
- Never Indicates the type of a value that Never exists
- For example, the never type is the return type of function expressions or arrow function expressions that always throw an exception or have no return value at all
// A function that returns never must have an unreachable end
function error(message: string) :never {
throw new Error(message);
}
function infiniteLoop() :never {
while (true) {}}Copy the code