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++一样,面向对象,编译的时候就会检查数据类型是否匹配,在编程过程中可以减少很多类型错误,比如下面的对象的检查:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值