Typescript other data types (2)

Table of contents

Object object

Then we want to add a dispensable attribute, how to write it?

Then we want to add multiple dispensable attributes, how to write?

How to define a function to limit the parameters?

Array Array

array type declaration

How do we want to define an array of any type?

special array - tuple

Enumeration enum

Replenish


Object object

// object表示一个js对象
let a: object;//一般不用
// 对象一
a = {};
// 对象二,function也代表一个对象
a = function () { };

In development, we generally don't care whether it is an object, but what attributes are contained in the object

// {} 用来指定对象中可以包含哪些属性,要求赋值的时候定义的结构一模一样,否则就会报错
// 语法:{属性名:属性值,xxx,xxx}
let b: { name: string };
b = {name:'小锁'}

 Note: {} is used to specify which attributes can be included in the object. When assigning values, the defined structure is exactly the same. One more or one less attribute will not work, otherwise an error will be reported

Then we want to add a dispensable attribute, how to write it?

Just add a question mark after the attribute name to indicate that the attribute age is optional . At this time, no matter whether we have one more age or one less age, no error will be reported.

Then we want to add multiple dispensable attributes, how to write?

Some people say, then I define it all

 This will show very cumbersome

At this point we can represent any property in a way

As follows:  [propName:string]:any  where propName can be named as any name, such as [xx:string]:any, it only represents the property name, and the whole represents any type of property.

How to define a function to limit the parameters?

can be written as an arrow function-like form

 At this time, we will not report an error when we define a specific function. Of course, one more or one less parameter, or the type is different will report an error.

 

 Summary: The type declaration of the setting function structure is as follows

 Syntax: (parameter 1: type 1, parameter 2: type 2,....) => return value

The subject is finished~


Array Array

array type declaration

1. Declare an array of numeric types, that is,  type + [] or Array<type>  can be

When there is non-numeric data in it, an error will be reported.

How do we want to define an array of any type?

Just use any. For example: let c:Array<any>, but it is not recommended to use any, try to be clear about the type

 At this time, the advantage of ts comes out. When we define an array, it will prompt all the methods related to the array for use.

When there is a type constraint, if we want to push a non-numeric type of data, an error will be reported

In js, there is a kind of array arguments , which is very similar to an array, but it is not an array. It does not have as many properties and methods as an array , for example

 When we assign arguments to arr, an error will also be reported, which shows that they are not of the same type

 


special array - tuple

A tuple is a fixed-length array, very similar to an array, except that the data type is written into the array.

In layman's terms, a tuple is an array that limits the data type to a certain extent.

Tuples themselves are also arrays, so all the properties and methods of arrays also exist

 When we push a string or a number, it is ok, because tuples can be considered as an array of union types , and when we push a boolean value, an error will be reported

 


Enumeration enum

List all possible results, and use enum to indicate that it is an enumeration type

// 枚举
enum Gender {
  male = 0,
  female = 1,
}
// 使用
// let i: { name: string; gender: 0 | 1 };
// 等价于
let i: { name: string; gender: Gender };
i = {
  name: "小锁",
  gender: Gender.male, // 等价于gender: 0
};

Replenish

(1) There are multiple properties in the object at the same time, which are restricted by the symbol &

 (2) Type aliases

If we want to define multiple possible values ​​for an attribute, it will be extremely cumbersome and repetitive to write like the following

let k: 1 | 2 | 3 | 4 | 5;
let l: 1 | 2 | 3 | 4 | 5;

How to solve it? We can simplify redundant code by setting type aliases

// 类型的别名
type myType = 1 | 2 | 3 | 4 | 5;
let k: myType;
let l: myType;
// 使用
k = 3

At this point, the data type of ts is finished, have you learned it~

Guess you like

Origin blog.csdn.net/qq_41579104/article/details/129088045