TypeScript is strongly typed, statically typed JavaScript

1. Classification of programming languages

As we all know, JavaScript is a weakly typed , dynamically typed programming language.
insert image description here
First, let's explain these terms:

  • Dynamic type language : A language that does data type checking only during runtime (Runtime).
  • Static type language : Data type checking is performed during compilation (Compile). C/C++/Java is a typical representative of static language.
  • Strongly typed language : Mandatory type definition, to modify its type, an explicit type conversion must be performed.
  • Weakly typed language : A language in which data types can be ignored. (A variable can be assigned different types of values)

For the distinction between static type and dynamic type, there is a famous saying:

Static typing when possible, dynamic typing when needed


2. Give an example to illustrate the characteristics of JavaScript

As mentioned above JavaScript is a weakly typed , dynamically typed programming language. Here we do a demo, let's make fun of it.

// 在这里,定义一个简单的求和函数
function sum(x,y){
    
    
	return x + y
}

insert image description here
These are the problems of weak typing . The most terrifying thing is that JavaScript is a dynamically typed language, and these errors must be found during runtime. ( The leaks are not repaired until the boat arrives in the middle of the river—too late )

At this time, senior people said that our JavaScript can also avoid the above problems:

// 这样做确实可以(只要你不怕麻烦)
function sum(x,y){
    
    
	 if(typeof(x) ==="number" && typeof(y)==="number"){
    
    
         return x + y
     }else{
    
    
         throw "invalid data"
     }
}

At this time, a classmate with strong type language experience made a speech - add data class to shape
insert image description here


// 第一种方案:Uncaught SyntaxError: Unexpected token ':'
function sum(x:number,y:number){
    
    
	return x + y;
}
// 第二种方案:ncaught SyntaxError: Unexpected identifier 'x'
function sum(number x, number y){
    
    
	return x + y;
}


Isn't there a solution to give JavaScript developers a sense of security?

3. The power of TypeScript

insert image description here

TypeScript has an unusual relationship with JavaScript. TypeScript provides all the functionality of JavaScript and adds a layer on top of it: TypeScript's type system .

Using TypeScript's type system, we can write code like this:

Type inference: By being aware of how JavaScript works, TypeScript can build a type system that accepts JavaScript code but has types. This type system eliminates the need for us to add extra characters to specify types explicitly .


// 创建变量并将其赋值给特定值时, TypeScript 将使用该值作为其类型。
const name:string = "BertKing"
// 类型推断
const name = "BertKing"

function sum(x:number,y:number):numbre{
    
    
	return x + y;
}

// 同理,TS具有类型推断能力,这里函数的返回值类型也可以省略
function sum(x:number,y:number){
    
    
	return x + y;
}

If we call sum(5,true), an error will be reported during compilation:
Use the TypeScript compiler at the terminal:
insert image description here
Use VSCode:
insert image description here
In short, TypeScript will report obvious errors before the code is executed, and the rigor it brings can be effective avoid many mistakes. This is the so-called static type checking of TypeScript

insert image description here
Strike while the iron is hot, let's get to know TypeScript Compiler (TSC) first

Guess you like

Origin blog.csdn.net/wangcheeng/article/details/128170683