“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
Front knowledge
Object represents a non-primitive type, that is, a type other than number, string, Boolean, symbol, NULL, or undefined.
Object class
There is no error when we declare it like this
let foo: object = function () {}let foo1: object = []
let foo2: object = { }
Copy the code
But we use it to declare a raw value and get an error
let foo3: object = false / / an error
Copy the code
How do I declare an object?
We can do this by declaring object arguments
let foo = {}
Copy the code
Of course we can add some restrictions
let foo: {name: string, age: number } = {name:'Tom'.age: 11 }
Copy the code
Of course you could write it this way
letfoo: {name? : string,age: number } = {name:'Tom' }
Copy the code
How do you define objects?
In TypeScript, we use Interfaces to define the types of objects. (More on that next time lol)
Array Array type
We can make a statement like this
let list: number[] = [1.2.3];
Copy the code
You can also use array generics
let list: Array<number> = [1, 2, 3];
Copy the code
Both methods represent an array of elements of this type
A tuple type
A tuple type is a special type that I understand to be an array with an explicit element type and number
The basic mold
// Let's define one
let x: [string, number];
Copy the code
The right example
x = ['hello'.10]; // OK
Copy the code
The wrong case
x = [10.'hello']; // Error
Copy the code
or
x = ['hello'.10.10]; // Error
Copy the code
Enumerated type
Enum types complement the JavaScript standard data types.
In JavaScript we often use an object to simulate enumerated classes.
let Status = {
dangurous: 0.safe: 1,}Copy the code
inTypeScript
How do you behave in?
enum Color {
Red,
Green,
Blue
}
let c: Color = Color.Green; / / 1
Copy the code
In the absence of any assignment, number the elements starting at 0 and add up. Using this feature we can also do the following
enum Color {
Red = 2,
Green,
Blue
}
let c: Color = Color.Green; / / 3
Copy the code
Of course if we do this we will get an error
enum Color {
Red = 'red',
Green, // Enum member must have initializer.
Blue // Enum member must have initializer.
}
let c: Color = Color.Green;
Copy the code
The main reason for this is that strings don’t grow by themselves, so we need to do a separate assignment
summary
This chapter is mainly and you introduced about the object type below some value operations, after we will give you some classes, interfaces, functions related articles