TypeScript static typing

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

One of the most important features of TypeScript is that you can define static types. The English name is Static Typing. What does that mean? Conceptual things that are too complicated will not be discussed here. You can simply understand the "static type", that is, once you define it, you can't change it. For example, if you are a man, you must be a man for the rest of your life; if you are a woman, you will be a woman, and you will be a woman for the rest of your life. This thing cannot be changed! Uh...It seems that you can change and change at will now, here is the normal situation. But it also has some features, this is not as simple as it seems. Let's learn now.

How to define static types

You can create a new file in the folder of the previous issue demo2.ts, and then write this code:

const count: number = 1;

This is the simplest way to define a countvariable of a numeric type , here : numberis to define a static type. After such definition, countthe variable will always be a number type in the program and cannot be changed. For example, if we countcopy a string at this time , it will report an error.

//错误代码
const count: number = 1;
count = "Code程序人生";

But this is only the simplest understanding. Going deeper, you will find that the countvariables at this time can use numberall the attributes and methods of the type. We can countsee this feature by typing a. at the back, and the editor will give you very good hints. This is one reason why I like to use an VScodeeditor.

Custom static type

You can also define a static type yourself. For example, you define a 小姐姐type now , and then you can use this static type when declaring a variable. See the code below.

interface XiaoJieJie {
    
    
  uname: string;
  age: number;
}

const xiaohong: XiaoJieJie = {
    
    
  uname: "小红",
  age: 18,
};

If you declare a variable at this time, it is not the same as a custom one, and an VSCodeerror will be reported directly. It should be noted that at this time the xiaohongvariable also has the unameand ageattribute.

What you need to remember in this issue is that if you use a static type, it not only means that the type of the variable cannot be changed, it also means that the properties and methods of the type are also determined. This feature greatly improves the robustness of the program, and the editor will also give you good grammatical hints at this time, speeding up your development efficiency.

As you continue to study in depth, you will have a deeper understanding of these two advantages.


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/115279161