当前位置:网站首页>Typescript type system
Typescript type system
2022-06-24 01:33:00 【HoneyMoose】
in the light of JavaScript The above question , Smart students think we'll give JavaScript Add a type , and Java equally , Be able to define the type of variable , The idea is TypeScript The type of system , To a great extent JavaScript The confusion caused by .
from TypeScript Your name can be seen ,「 type 」 Is its core feature ,TypeScript It is also mainly committed to solving JavaScript The type of confusion problem .
TypeScript Is a static type
The type system follows 「 The timing of the type check 」 To classify , It can be divided into the following 2 Kind of
- Dynamic type
- Static type
Dynamic type This means that type checking is performed only at run time , The wrong type of language often leads to runtime errors . JavaScript It's an interpretive language , There is no compile phase ( This is another target Java Students often make complaints about their places. ), So it's a dynamic type , The following code will only report an error at run time :
let foo = 1;
foo.split(' ');
// Uncaught TypeError: foo.split is not a function
// The runtime will report an error (foo.split Not a function ), Caused during operation bug.
// Open your browser F12 Look at how many mistakes there are, and you'll see .Static type It means that the type of each variable can be determined at the compilation stage , The wrong type of language often leads to grammatical errors .TypeScript Before running, you need to compile to JavaScript, Type checking is done at the compile stage , therefore TypeScript Is a static type , This paragraph TypeScript The code will report an error at the compilation stage :
let foo = 1;
foo.split(' ');
// Property 'split' does not exist on type 'number'.
// Error will be reported at compile time ( The numbers don't have split Method ), Unable to compile You may be surprised , This paragraph TypeScript The code looks like JavaScript There's no difference .
you 're right ! Most of the JavaScript The code needs only a small amount of modification ( Or no modification at all ) It becomes TypeScript Code , Thanks to TypeScript Powerful [ type inference ][], Even if you don't declare variables manually foo The type of , It can also automatically infer that it is a number type .
complete TypeScript The code looks like this :
let foo: number = 1;
foo.split(' ');
// Property 'split' does not exist on type 'number'.
// Error will be reported at compile time ( The numbers don't have split Method ), Unable to compile TypeScript It is hoped that this can be enhanced through the above configuration JavaScript The function of .
边栏推荐
- Software cost evaluation: a method for estimating software scale by fast function point method
- How to select storage space for website construction what factors should be considered in selecting space
- Part of the problem solution of unctf2020
- Tke cluster setup linkerd2
- Software cost evaluation: basic knowledge interpretation of cosmoc method
- SMS marketing is the key to retain customers
- Feasibility of importing UE4 using gltf with instances
- Note 3 of disruptor: basic operation of ring queue (without disruptor class)
- "Ai+ education" and "Ai education": one for education and the other for Education
- How does the fixed assets management software reduce costs and increase efficiency for enterprises?
猜你喜欢
随机推荐
How to realize IP invariance in the private network of basic network ECs and cloud database resource switching
Salesforce batch apex batch processing (I)
EasyPlayer. JS play m3u8 error net:: err_ EMPTY_ Response, how to solve it?
LMS Virtual. Derivation method of lab acoustic simulation results
How to build a practical website and how to operate after the website goes online
2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
How to choose a website construction company self-study website or website construction company
Code practice | ground extraction with logo-loam
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
Echo framework: implementing distributed log tracing
CSDN auto sign in
Application analysis of video edge computing gateway easynvr in video overall monitoring solution
Introduction to easycvr interfacing with Huawei IVS subscription camera and user change request interface
Note sharing (5) -precautions for Oracle to MySQL
How is the national standard easygbs video technology applied in the comprehensive supervision scenario of the power supply business hall?
How to self-study website construction is website construction company reliable
How to build a "preemptive" remote control system (- - memory chapter)
[planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
PHP implementation of interval sorting of classified data
Moment. JS how to get the zero hour time of the current time
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
