foreword

TypeScript is popular, it's a well-known thing to front-end practitioners.

TypeScript is very easy to use. However, many large front-end frameworks have been refactored to use TypeScript.

In order not to fall behind, I also need to systematically learn about TypeScript. Today is the first lesson.

The concept and significance of TypeScript

TypeScript is a free and open source programming language developed by Microsoft. It is built by adding static type definitions on top of JavaScript. TypeScript is translated into JavaScript code by the TypeScript compiler or Babel, and can run on any browser and any operating system.

  • TypeScript is JavaScript with an added type system for projects of any size.
  • TypeScript is a statically typed, weakly typed language.
  • TypeScript is fully compatible with JavaScript, it does not modify the characteristics of the JavaScript runtime.
  • TypeScript can be compiled to JavaScript and run in browsers, Node.js, and any other environment that can run JavaScript.
  • TypeScript has many compilation options, and the strictness of type checking is up to you.
  • TypeScript can coexist with JavaScript, which means that JavaScript projects can migrate to TypeScript incrementally.
  • TypeScript enhances the functionality of the editor (IDE), providing capabilities such as code completion, interface hints, jumping to definitions, and code refactoring.
  • TypeScript has an active community, and most commonly used third-party libraries provide type declarations.
  • TypeScript evolves in step with the standard and conforms to the latest ECMAScript standard (stage 3)

TypeScript compiles clean, concise JavaScript code that runs on any browser, in a Node.js environment, and in any JavaScript engine that supports ECMAScript 3 (or higher).

TypeScript has more rules and type restrictions, and the code is more predictable, controllable, and easy to maintain and debug; it supports modules, namespaces, and object orientation, making it easier to organize code to develop large and complex programs.

TypeScript's compile step can catch errors before running.

image-20220221182629919

Key Features of TypeScript

As can be seen from the above concepts and meanings, TypeScript has many features. Here we will analyze several main features.

TypeScript is statically typed

We all know that JavaScript is an interpreted language, there is no compilation phase, it only performs type checking at runtime.

TypeScript is the opposite of it, because TypeScript also goes through the process of being compiled into JavaScript before running. It can determine the type of each variable at the compilation stage. Type errors in this language often lead to syntax errors.

For example, in JavaScript:

let foo = 1;
foo.split(' ');
// Uncaught TypeError: foo.split is not a function
// 运行时会报错(foo.split 不是一个函数),造成线上 bug

Whereas in TypeScript:

let foo = 1;
foo.split(' ');
// Property 'split' does not exist on type 'number'.
// 编译时会报错(数字没有 split 方法),无法通过编译

TypeScript is weakly typed

At this point, the behavior of TypeScript and JavaScript is the same, which means that data types can be implicitly converted. See the following code:

console.log("1" + 1);
// // 打印出字符串 '11'

The output is the same whether in TypeScript or JavaScript.

Install TypeScript globally

npm install -g typescript

Summarize

Today, I will mainly get to know TypeScript, know what it is and what it is useful for, and then learn how to use it.

~

~ This article is over, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, and shape interesting souls!

Hello everyone, I'm King , the author of 〖 Programming Samadhi 〗, my official account is " Programming Samadhi ", welcome to pay attention, I hope you can give me more advice!


编程三昧
54 声望10 粉丝

学习有趣的知识,交识有趣的朋友,造就有趣的灵魂!