In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the top functions of TypeScript". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is TypeScript
TypeScript is a popular application in recent years, which gives us an illusion: with so many supporters, is TypeScript a new language?
TypeScript is an object-oriented programming language developed and maintained by Microsoft. It is a superset of JavaScript and contains all its elements.
TypeScript completely follows the concept of OOPS. With the help of TSC (TypeScript compiler), we can convert TypeScript code (.ts file) into JavaScript (.js file).
TypeScript is a superset of JavaScript
II. A brief history of TypeScript
Anders Hejlsberg (the creator of TypeScript) began developing TypeScript at Microsoft in 2010 and released the first version of TypeScript (TypeScript 0.8) to the public in 2012. Although the release of TypeScript has been praised by many people around the world, it has not been mainly adopted by the JavaScript community due to the lack of support from the major ide.
The first version of TypeScript (TypeScript 0.8) was released in October 2012.
The latest version of Typescript (Typescript 3.0) was released in July 2018, and you can download the latest version here!
Third, why do we use TypeScript?
TypeScript simplifies the JavaScript code, making it easier to read and debug.
TypeScript is open source.
TypeScript provides efficient development tools for JavaScript ide and practices such as static checking.
TypeScript makes the code easier to read and understand.
With TypeScript, we can greatly improve the normal JavaScript.
TypeScript provides us with all the advantages of ES6 (ECMAScript 6), as well as higher productivity.
By checking the type of code, TypeScript can help us avoid the painful errors we often encounter when writing JavaScript.
A powerful type system, including generics.
TypeScript is just a JavaScript with some additional features.
TypeScript code can be compiled according to ES5 and ES6 standards to support the latest browsers.
Align with ECMAScript for compatibility.
Start and end with JavaScript.
Static types are supported.
TypeScript will save developers time.
TypeScript is a superset of ES3, ES5, and ES6.
Additional features of TypeScript
A function with optional arguments.
A function that takes the REST argument.
Generic support.
Module support.
Fourth, Daniel speaks for himself:
"there are many ways we like TypeScript... with TypeScript, several of our team members said similar things, and I actually understand most of our own code now! because they can easily traverse it and better understand relationships. We have found several vulnerabilities through TypeScript inspection." -Director of Engineering, Brad Green,Angular "
One of the main goals of Ionic is to make application development as fast and simple as possible, and tool support TypeScript provides us with autocompletion, type checking, and true consistency of source documents. "- Tim Lancina, tool developer-Ionic"
TypeScript is a wise choice when writing modern applications based on web or JavaScript. TypeScript's carefully considered language features and functions, as well as its constantly improving tools, have led to a very productive development experience. "- Epic researcher Aaron Cornelius"
TypeScript helps us reuse team knowledge and maintain the same team speed by providing the same excellent development experience as C #. It is a great improvement over ordinary JavaScript. "- Valio Stoychev,PM Lead-NativeScript"
Fifth, top-level functions of TypeScript that you may not know
1. Object-oriented programming
TypeScript contains a very good set of object-oriented programming (OOP) features that help maintain robust and clean code; this improves code quality and maintainability. These OOP features make the TypeScript code very neat and organized.
For example:
Class CustomerModel {
CustomerId: number
CompanyName: string
ContactName: string
Country: string
}
Class CustomerOperation {
AddCustomer (customerData: CustomerModel): number {
/ / add users
Let customerId = 5 / ID returned after saving
Return customerId
}
}
2. Interface, generics, inheritance and method access modifiers
TypeScript supports interfaces, generics, inheritance, and method access modifiers. Interfaces are a good way to specify contracts. Generics help provide compile-time checking, inheritance makes new objects have properties of existing objects, and access modifiers control the accessibility of class members. TypeScript has two access modifiers-public and private. By default, members are public, but you can explicitly add public or private modifiers to them.
(1) Interface
Interface ITax {
TaxpayerId: string
CalculateTax (): number
}
Class IncomeTax implements ITax {
TaxpayerId: string
CalculateTax (): number {
Return 10000
}
}
Class ServiceTax implements ITax {
TaxpayerId: string
CalculateTax (): number {
Return 2000
}
}
(2) access modifier
Class Customers {
Public companyname:string
Private country:string
}
Display a public variable and a private variable
(3) inheritance
Class Employee {
Firstname:string
}
Class Company extends Employee {
Department:string
Role:string
Private AddEmployee () {
This.Department= "myDept"
This.Role= "Manager"
This.FirstName= "Test"
}
}
(4) generics
Function identity (arg: t): t {
Return arg
}
/ / shows an example of a generic implementation
Let output = identity ("myString")
Let outputl = identity (23)
(5) strong / static type
TypeScript does not allow mixing values with different data types. If these restrictions are violated, an error will be thrown. Therefore, types must be defined when variables are declared, and no values can be assigned other than those that are likely to be defined in JavaScript.
For example:
Let testnumber: number = 6
Testnumber = "myNumber"; / / this will raise an error
Testnumber = 5; / / that's fine.
3. Compile-time / static type checking
If we do not follow the correct syntax and semantics of any programming language, the compiler will throw a compile-time error. They do not cause the program to execute a line of code until all syntax errors or debug compile-time errors are removed. The same is true of TypeScript.
For example:
Let isDone: boolean = false
IsDone = "345"; / / this will cause an error
IsDone = true; / / that's it.
4. Less code than JavaScript
TypeScript is a wrapper for JavaScript, so you can use helper classes to reduce code. The code in Typescript is easier to understand.
5. Readability
Interfaces, classes, and so on provide readability for the code. Because the code is written in classes and interfaces, it makes more sense and is easier to read and understand.
For example:
Class Greeter {
Private greeting: string
Constructor (private message: string) {
This.greeting = message
}
Greet () {
Return "Hello," + this.greeting
}
}
JavaScript Code:
Var Greeter = (function () {)
Function Greeter (message) {
This.greeting = message
}
Greeter.prototype.greet = function () {
Return "Hello," + this.greeting
}
Return Greeter
) ()
6. Compatibility
Typescript is compatible with JavaScript libraries, such as underscore.js,Lodash, etc. They have many built-in and easy-to-use features that make development faster.
7. Provide a "compiler" that can convert code to JavaScript equivalent code
The TypeScript code consists of pure JavaScript code and some keywords and constructs specific to TypeScript. However, when the TypeScript code is compiled, it is converted to normal JavaScript. This means that the generated JavaScript can be used with any browser that supports JavaScript.
8. Support module
As the TypeScript code base grows, it becomes important to organize classes and interfaces for better maintainability. The TypeScript module allows you to do this. Modules are containers of code that can help you organize your code in a neat manner. Conceptually, you may find them similar to .NET namespaces.
For example:
Module Company {
Class Employee {
}
Class EmployeeHelper {
TargetEmployee: Employee
}
Export class Customer {
}
}
Var obj = new Company.Customer ()
9. ES6 feature support
Typescript is a superset of ES6, so it has all the features of ES6. There are also some features, such as support for arrow functions commonly referred to as lambda functions. ES6 introduces a slightly different syntax to define anonymous functions, called fat arrow syntax.
For example:
SetTimeout () = > {
Console.log ("setTimeout called!")
}, 1000)
10. Use in popular frameworks
TypeScript has become more and more popular in the past few years. Perhaps the defining moment of TypeScript popularity is the official transition from Angular2 to TS, which is a win-win situation.
11. Reduce errors
It reduces errors such as null handling, undefined, and so on. Strongly typed features that restrict developers to write specific types of code through appropriate type checking.
12. Function overloading
TypeScript allows you to define overloaded functions. In this way, you can call different implementations of the function based on the parameters. Keep in mind, however, that TypeScript function overloading is a bit odd and requires type checking during implementation. This limitation is due to the fact that TypeScript code is eventually compiled into pure JavaScript, while JavaScript does not support the true concept of function overloading.
For example:
Class functionOverloading {
AddCustomer (custId: number)
AddCustomer (company: string)
AddCustomer (value: any) {
If (value & & typeof value = = "number") {
Alert ("First overload -" + value)
}
If (value & & typeof value = = "string") {
Alert ("Second overload -" + value)
}
}
}
13. Constructor
Classes defined in TypeScript can have constructors. The constructor usually initializes the object by setting the default value to its property. Constructors can also be overloaded like functions.
For example:
Export class SampleClass {
Private title: string
Constructor (public constructorexample: string) {
This.title = constructorexample
}
}
14. Debugging
Code written in TypeScript is easy to debug.
15. TypeScript is just JavaScript
TypeScript begins with JavaScript and ends with JavaScript. Typescript uses the basic building blocks of the program in JavaScript. For execution purposes, all type script code is converted to its JavaScript equivalent.
For example:
Class Greeter {
Greeting: string
Constructor (message: string) {
This.greeting = message
}
Greet () {
Return "Hello," + this.greeting
}
}
JavaScript Code:
Var Greeter = (function () {)
Function Greeter (message) {
This.greeting = message
}
Greeter.prototype.greet = function () {
Return "Hello," + this.greeting
}
Return Greeter
) ()
16. Portability
TypeScript is portable across browsers, devices, and operating systems. It can run in any environment in which JavaScript runs. Unlike the corresponding script, TypeScript does not require a dedicated VM or a specific runtime environment to execute.
This is the end of the content of "what are the top features of TypeScript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.