Toc
  1. 强类型与弱类型
  2. 静态类型和动态类型
  3. 使用TypeScript
    1. 安装TypeScript
    2. 创建TypeScript配置文件
    3. 第一句’Hello TypeScript’
Toc
0 results found
Ihuzb
TypeScript学习笔记 入门知识

强类型与弱类型

  • 强类型语言:不允许改变变量的数据类型,除非进行强制类型转换。
    • 严格的限制,可以避免很多低级错误。Java
  • 弱类型语言:变量可以被赋予不同的数据类型。
    • JS就是弱类型语言,虽然灵活,但是会造成bug。
let x = 1;
let y = true;
x = y;
//true
let z = 'a';
x = z;
//'a'

静态类型和动态类型

  • 静态类型语言:在编译阶段确定所有变量的类型。
    • 对类型极度严格,立即发现错误,运行时性能好,自文档化
//C++
class C{
public:
int x;
int y;
}

int add(C a,C b){
return a.x + a.y + b.x + b.y
}
  • 动态类型语言:在执行阶段确定所有变量的类型。
    • 对类型非常宽松,Bug可能隐藏数月甚至数年,运行时性能差,可读性差。
//JS
class C {
constructor(x, y) {
this.x = x;
this.y = y;
}
}

function add(a, b) {
return a.x + a.y + b.x + b.y
}

使用TypeScript

安装TypeScript

//全局安装TypeScript
# npm i typescript -g

创建TypeScript配置文件

# tsc --init
//生成tsconfig.json文件,ts的所有配置都可以在这里面设置

第一句’Hello TypeScript’

  • 创建src目录,新建index.ts文件,写入代码;
let hello: string = 'Hello TypeScript';
  • 编译ts文件,在控制台执行以下代码;
# tsc ./src/index.ts
//生成index.js文件。
  • 生成的index.js文件内容;
var hello = 'Hello TypeScript';
本文作者:Ihuzb
版权声明:本文首发于Ihuzb的博客,转载请注明出处!