In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is TypeScript? in view of this question, this article introduces in detail the corresponding analysis and answers, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Foreword:
TypeScript is a superset of JavaScript that can be compiled into pure JavaScript code.
Then why the emergence of the TypeScript language, mainly because the current JavaScript can develop a lot of complex projects, but JavaScript lacks its reliability, we need to add a lot of business logic to judge for the robustness of the code.
TypeScript can run in a browser environment, a Node.js environment, or an engine of ECMAScript3 or higher JavaScript.
The problem with 1.JavaScript
The main problems of JavaScript are as follows:
Type exceptions to JavaScript can only be found at run time.
Because the type of the function of JavaScript is not clear, it may cause problems with the final function of the function.
The code is as follows:
Advantages of function sum (x, y) {return x + y} sum (100,100') / / 1001002.TypeScript
JavaScript is a dynamically typed programming language. The so-called dynamic type only knows whether its data type is Number or String; at compile time, while TypeScript is a statically typed programming language. The so-called static type is to know what its data type is at the time of writing, and the scale defines a variable.
Let num: number = 6
The variable num can only be of type number from beginning to end, and if you assign a string to it, an exception will be thrown.
So the advantages of TypeScript are as follows:
In the development process, we can locate the wrong place, so it is convenient for us to check the error.
TypeScript is a progressive programming language, and if you don't know its syntax, you can use it completely as JavaScript.
It reduces unnecessary type checking in the development process.
Code hints for static types are superior to static type code hints.
It will be easier when refactoring a project.
The code semantics of static types are better and more readable than dynamic types.
Shortcomings of 3.TypeScript
TypeScript has not only advantages and no disadvantages. Its disadvantages are all relative to JavaScript.
The details are as follows:
Compared to JavaScript, TypeScript itself adds a lot of concepts. For example, concepts such as generics, interfaces, etc.
Developing with TypeScript increases some costs in the short term, but for a project that requires long-term maintenance, TypeScript can reduce maintenance costs.
It may not combine perfectly with some libraries.
The operating environment of 4.TypeScript
TypeScript runs on top of the Node.js environment, so you need to install Node.js first.
Install a series of actions such as Node.js to ignore
The command to install TypeScript is as follows:
Npm install-g typescript@3.6.4
Here I specify the version number through @, or I can not specify the version number.
After installing TypeScript, you also need to install a tool for ts-node. If you install this tool, you can't run the TS code directly. You need to program the TS code into JavaScript before it can be executed.
The execution process is as follows:
# compile TS code tsc demo.ts # after compilation, you will get the demo.js file, and then you can run it
If you have installed the node-ts tool, you can execute the TS code directly.
The specific steps are as follows:
# 1. Global installation of npm install-g ts-node@8.4.1# 2. Run the code ts-node demo.ts
It is worth noting that the directory after installation must be in the environment variable, otherwise an error will be reported.
5. Scope problem
When we execute the ts file in the project, we will throw an exception if the same variable name exists in different files.
The sample code is as follows:
A.tslet str: string = 'Hello World'b.tslet str: string =' one bowl week'
An exception is thrown that the block scope variable "str" cannot be redeclared, and if the editor is VScode, hover over the variable name to prompt.
There are two ways to solve this problem. The first is to create an immediate execution function (that is, an anonymous function) for each file, ensuring that each file has a separate scope.
The sample code is as follows:
(function () {let str: string = 'one bowl week'}) {}
The second way is to use export to export the current file as a module. The sample code is as follows:
Let str: string = 'Bowl week' export {} this is the answer to the question about what TypeScript is. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.