TypeScript basic static types and object types

Codewords are not easy, and helpful students hope to pay attention to my WeChat public account: Code program life, thank you! The code is self-used and fetched.

Insert picture description here

There are two types of static types in TypeScript, one is the basic static type, and the other is the object type. Both of these are often used, which is very important. Let's take a look at what is the basic static type.

Basic static typing

The basic static type is very simple, just add a :sign after the declared variable , and then add the corresponding type. For example, the following code declares a numeric variable called count.

const count : number = 918;
const myName :string = 'jspang'

Like this commonly used type of foundation there, I am here to give you a few of the most commonly used oh, null, undefinde, symbol, boolean, voidthese are the most commonly used basic data types, as for example, I will not detail here wrote, hit back, Let's continue to explain.

Object type

Let's take a look at an example first. The experienced friends will know the general idea through the example, and then we will explain it (in fact, we also talked about it in the last class, and we will review it here). Create a new file demo3.ts(you can be different from me), and then write the following code.

const xiaoJieJie: {
    
    
  name: string,
  age: number,
} = {
    
    
  name: "大脚",
  age: 18,
};
console.log(xiaoJieJie.name);

After writing, we terminalinput in (terminal), ts-node demo3.tsand we can see the result output 大脚. This is a classic object type and the simplest object type. The object type can also be an array. For example, now we need a lot of young ladies, we can write like this.

const xiaoJieJies: String[] = ["谢大脚", "刘英", "小红"];

The meaning at this time is that the variable xiaoJieJiesmust be an array, and the content of the array must be a string. You can try to change the string to a number, it VSCodewill report an error directly to us.

const xiaoJieJies: String[] = ["谢大脚", "刘英", 123];

Now I pay attention to object-oriented programming. I have been object-oriented programming for so many years, and I haven't compiled another one. Let's take a look at the following code again. This code uses the form of a class to define variables.

class Person {
    
    }
const dajiao: Person = new Person();

This means that it dajiaomust be an Personobject corresponding to a class. We can also define a function type and determine the return value. code show as below:

const jianXiaoJieJie: () => string = () => {
    
    
  return "大脚";
};

Then we summarize now that the object type can have several forms:

  • Object type
  • Array type
  • Class type
  • Function type

These forms are TypeScriptcalled object types in here.

In this lesson, we have mainly learned the concepts of basic types and object types. We hope that all friends can learn it. Once again, we remind that hands-on practice will have better results. Friends, come on.


There is a WeChat mini program course design, complete design needs, contact personal QQ: 505417246

Pay attention to the following WeChat public account, you can receive WeChat applet, Vue, TypeScript, front-end, uni-app, full-stack, Nodejs, Python and other practical learning materials. The
latest and most complete front-end knowledge summary and project source code will be released to the WeChat public as soon as possible Number, please pay attention, thank you

After paying attention to the official account , reply to the front-end interview questions and receive a large number of front-end interview questions summary pdf data
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46171043/article/details/115286504