What is TypeScript

TypeScript is an open source, cross-platform programming language developed by Microsoft. It is a superset of JavaScript that will eventually be compiled into JavaScript code.

An introduction to TypeScript

It is a superset of JavaScript, and essentially adds optional static typing and class-based object-oriented programming to the language.

3. TypeScript usage scenarios

TypeScript extends JavaScript’s syntax so that any existing JavaScript program can run in a TypeScript environment. TypeScript is designed for large application development and can be compiled into JavaScript.

What’s good about TypeScript

1. Prompt system of type system to assist development tools (especially team development). For example, when we call a public function, the editor can clearly tell you how many arguments the function needs, what types of arguments are required, and which arguments are optional. This can save a lot of consulting, communication costs, improve the efficiency of development. 2. Identification of error codes to prevent automatic type conversion. In the process of writing code, we can timely find the errors caused by type conversion, whether the method call is comprehensive and the parameter type is correct. Maintaining a good system is to write fewer new bugs. Conclusion: Enjoy the benefits of static typing, such as IDE's full development assistance and strict code review; And make your code as simple and flexible as Javascript!Copy the code

Common uses of TypeScript

Boolean 2.number 3.array 4.null 5.undefined 6.any 7.voidCopy the code

// Declare an interface type

interface Params {
    tag1: number, // Mandatory type
    tag2: { // Object type Specifies the type of the property within the object
        name: string,
        length: number }, tag3 ? : string,// Select the transmission type
    readonly tag4 : string, // It can only be read after initialization
    [propName: string]: any // There may be other unknown attributes attributes of any type
}

interface AddParam {
    moreTags: string 
}

interface OwnerParams extends Params, AddParam{
    special: boolean // Interface type combination
}
Copy the code

For example 🌰, when we define an object, we will automatically prompt it with the properties and the type values of those properties when we use the object.

When we remove the unknown type, declaring a new attribute on the current object will prompt an error 🙅♂️

The point is, when we’re working with multiple people, using TS lets us know what structure type the method passes and what structure type the method returns. This will reduce errors in communication, Code TalksIt is easier to find errors and write fewer bugs in development calls

TS only needs to write more type definition code during development to help us avoid the trouble of type conversion. The actual compilation is converted to javascript, and the actual amount of code deployed does not increase. TS is relatively easy to get started. You can start with basic data types to lay the foundation for large projects.