This is the first day of my participation in the August Text Challenge.More challenges in August

As you know, the first article in every new series is a brief introduction to TypeScript, and this one is no exception

TypeScript

The following links are useful for learning about typescript, especially in the book stack, where you can get an inside look at typescript

TypeScript learning Links:

  • www.tslang.cn/index.html
  • Chinese.freecodecamp.org/forum/t/top…
  • www.bookstack.cn/explore?cid…
  • Basarat. Gitbook. IO/typescript /…

Introduce TypeScript

TypeScript is a superset of JavaScript with its statically typed features, also known as JavaScript that Scales

Why TypeScript is used

JavaScript is not suitable for large application development. JavaScript is a dynamic language with no type system, and variables can be of any type

A type system improves code quality and readability, making the code base easier to maintain or refactor. Most importantly, errors can be caught at compile time, rather than at run time.

TypeScript checks the correctness of different parts of code at compile time. Errors are detected at compile time so that developers can find out where the error is and what the problem is, and if errors are detected only at run time, you need to trace a complex stack for debugging.

Advantages and disadvantages TypeScript

Advantages:

  • Potential errors can be caught earlier in the development cycle
  • Manage a large code base
  • Easier to refactor
  • Easier teamwork: The more coupled the code, the less likely it is to cause unintentional damage when different developers access the code base
  • Document features: The type itself is a kind of document information, convenient for the developer to query
  • TypeScript embraces the ES6 specification and also supports some of the ESNext draft specifications

Disadvantages:

  • Additional learning required: tradeoffs between short-term slowdowns and long-term efficiency gains
  • Type errors can vary
  • Configuration greatly affects operation
  • It may not work perfectly with some libraries

TypeScript installation

TypeScript is easy to install and can be downloaded using NPM (PS: generally installed globally)

npm:

npm install -g typescript

cnpm:

cnpm install -g typescript

yarn:

yarn global add typescript

To check whether the installation is complete, run the tsc-v command on the terminal to check the version

The TypeScript file suffix is.ts, and running the command in CMD is TSC filenames.ts. This command does not run the ts file directly, but instead generates a JS file with the same name in the current folder.

TypeScript type system

TypeScript has two main goals:

  • Provides an optional type system for JavaScript
  • Provides functionality from future JavaScript versions to the current JavaScript engine

Types have been proven to improve code quality and understandability. (Big teams like Google, Microsoft, And Facebook keep coming to this conclusion.)

In C language, Java language and other strongly typed languages, the definition of variables such as numeric strings is strict, using int, string definition. But in JavaScript, you use var, let, and so on.

Strongly typed language: a language that enforces data type definition. That is, once a variable is assigned a data type, it will remain that data type forever unless cast. For example, if you define an integer variable a, it is impossible for the program to treat a as a string. Strongly typed languages are type-safe languages.

Weak type definition language: a language in which data types can be ignored. In contrast to strongly typed languages, a variable can be assigned values of different data types. Strong typing languages may be slightly slower than weak typing languages, but the rigor that comes with strong typing languages can effectively prevent many errors. Also, there is absolutely no connection between “is this a dynamic language” and “is this a type-safe language”!

  1. When refactoring, types improve agility. The compiler catches errors better than it does at run time
  2. Types are one of the best forms of documentation you can have. The function signature is a theorem, and the function body is a proof

Some key points:

  • TypeScript Type system design is optional, JavaScript is TypeScript;
  • TypeScript doesn’t prevent JavaScript from running, even with type errors, which allows JavaScript to migrate to TypeScript over time.