Typescript handbook1:Introduction详解

一、typescript——Introduction

typescript官方:https://www.typescriptlang.org/docs/handbook/intro.html

1.Introduction

The goal of TypeScript is to be a static typechecker for JavaScript programs - in other words, a tool that runs before your code runs (static) and ensure that the types of the program are correct (typechecked).
Typescript是JavaScript在被执行之前的类型检查工具,确保代码在运行之前的类型没有错误,例如两种语言变量赋值的不同:
JavaScript:

let r = 5;
let dream = true;
dream = r;
console.log(dream)  

打印出dream为5,dream的类型从布尔值直接通过被整数变量赋值的方式改变了数据类型,这在typescript里是不允许的,两个变量只能是在不同数据类型的时候给另一个赋值也是允许的。

typescript:
两个变量只能是在相同数据类型的时候给另一个赋值

if(dream === true){
    const aboolean:boolean = dream;  //if can assign to boolean type
    const astring:string = maybe;
    //Error:Type boolean' is not assignable to type 'string'.
}

2.The goals after you read the handbook

A reader who completes the walkthrough should be able to:
Read and understand commonly-used TypeScript syntax and patterns
Explain the effects of important compiler options
Correctly predict type system behavior in most cases
Write a .d.ts declaration for a simple function, object, or class

3.Relationship between typescript and javascript

TypeScript offers all of JavaScript’s features, and an additional layer on top of these: TypeScript’s type system.

For example, JavaScript provides language primitives like string, number, and object, but it doesn’t check that you’ve consistently assigned these. TypeScript does.This means that your existing working JavaScript code is also TypeScript code. The main benefit of TypeScript is that it can highlight unexpected behavior in your code, lowering the chance of bugs.

typescript和javascript之间的区别在于typescript是强类型语言,javascript是弱类型语言,typescript是javascript的超集。typescript就像C++一样,面向对象,编译的时候就会检查数据类型是否匹配,在编程过程中可以减少很多类型错误,比如下面的对象的检查:
在这里插入图片描述

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页