当前位置:网站首页>Typescript is a weak type
Typescript is a weak type
2022-06-24 01:34:00 【HoneyMoose】
The type system follows 「 Whether to allow implicit type conversion 」 To classify , It can be divided into strong type and weak type .
The following code, whether in JavaScript In or out TypeScript All of them can work normally , Run time numbers 1 Will be implicitly typed into a string '1', plus + Recognized as string splicing , So it prints out as a string '11'.
console.log(1 + '1'); // Print out the string '11'
TypeScript It's fully compatible JavaScript Of , It will not be modified JavaScript Runtime features , therefore They are all weak types .
Weakly typed languages correspond to strongly typed languages , for instance Java. Strongly typed language is a language that enforces type definition , That is, once a variable is defined as a type , Without coercion , Then it will always be this damn data type . Strong languages include :Java、.net、Python、C++ Other languages .
Although sometimes Java It will also give you some implicit conversions , But in most cases, the types do not match , An error will be reported when compiling .
for example , We can look at the following Python Code , because Python It's a strong type , The following code will report an error when running :
print(1 + '1') # TypeError: unsupported operand type(s) for +: 'int' and 'str'
To fix this error , You need to cast :
print(str(1) + '1') # Print out the string '11'
strong / Weak is relative ,Python When dealing with the addition of integers and floating points , Will implicitly convert an integer to a floating point , But it doesn't affect Python Is a strongly typed conclusion , Because most of the time Python There is no implicit type conversion . Compared with ,JavaScript and TypeScript No matter what type is on both sides of the plus sign , Can calculate a result through implicit type conversion —— Instead of reporting a mistake —— therefore JavaScript and TypeScript All weak types .
although TypeScript There is no restriction on the type of both sides of the plus sign , But we can use TypeScript Type system provided , as well as ESLint Code checking function provided , To restrict both sides of the plus sign to be numbers or strings . To some extent, this makes TypeScript towards 「 Strong type 」 One step closer —— Of course , This limitation is optional .
This type system embodies TypeScript Core design concept of : In complete preservation JavaScript Based on runtime behavior , Improve the maintainability of code by introducing static type system , Reduce the possibility of bug.
That's why it's right Java Let's talk about it , Maybe I prefer TypeScript some .
边栏推荐
- Moment. JS how to use epoch time to construct objects
- How to build high quality and high brand websites what should be paid attention to in the construction of enterprise websites
- How is the national standard easygbs video technology applied in the comprehensive supervision scenario of the power supply business hall?
- Salesforce uses hyperlink formula field to implement custom jump
- 5-step method to quickly find data analysis ideas
- Echo framework: implementing distributed log tracing
- Istio practice manual | meeting the new generation of microservice architecture
- . Net core cross platform development bbs forum (connotation source code + complete operation video)
- The Mars rescue plan has been released. The year of the tiger is limited to dolls waiting for you!
- What is the website construction process? What details need to be valued?
猜你喜欢
随机推荐
[flutter] comment utiliser les paquets et plug - ins flutter
Qu'est - ce que le financement des pensions? Quels sont les produits financiers pour les personnes âgées?
Software cost evaluation: basic knowledge interpretation of cosmoc method
[technology planting grass] skillfully use cloud function to create wechat web page authorization public service
Spatial4j introduction practice
[solution] how to realize AI automatic recognition of high altitude parabolic behavior?
Dart series part: asynchronous programming in dart
[technical grass planting] cdn+ lightweight server +hugo= let the blog "cloud native"
[planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
Devops culture: Amazon leadership principles
2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
Salesforce uses hyperlink formula field to implement custom jump
4 most common automated test challenges and Countermeasures
Security | how does Tencent virtual machine realize tamper proof of web pages?
Remove the cloud disk service display "continued" logo
How to quickly convert stock code into int in quantitative trading?
SAP executes PGI on the delivery order of STO and reports an error -fld selectn for Mvmt type 643 acct 400020 differences
Textplus - reverse engineering of textplus
What is the cost of domain name trademark registration? What is the use of domain names and trademarks?
November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path

