TypeScript(一)— 基础

前言

全局安装

1
npm install typescript -g

新建项目 typescript/
初始化项目

1
npm init -y

查看版本

1
tsc --version

生成 typescript 配置文件

1
tsc --init

node的类型声明

1
npm install @typesnode -S

配置 package.json

1
2
3
4
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch"
}

执行编译文件 npm run build

文档

http://www.zhufengpeixun.cn/ahead/html/65.1.typescript.html
http://www.zhufengpeixun.cn/ahead/html/65.2.typescript.html
http://www.zhufengpeixun.cn/ahead/html/65.3.typescript.html

知识点

super

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 1.ts
namespace a{
class Animal{
// 关于继承跟静态没有关系
static getAge(){
return '父类的静态方法'
}
getName(){
console.log("父亲的名称");
}
}
class Cat extends Animal{
// 这里子类不能继承父类的静态方法 super.getAge() X
// 只能 Animal 访问getAge()
getName():string{
return super.getName() + '--儿子的名称';
}
}
}

解析生成的部分代码 1.js

1
2
3
4
5
6
7
8
9
10
11
12

function Animal() {
}
Animal.prototype.getName = function () {
console.log("父亲的名称");
};
function Cat() {
return _super !== null && _super.apply(this, arguments) || this;
}
Cat.prototype.getName = function () {
return _super.prototype.getName.call(this) + '--儿子的名称';
};

super 继承的问题 ??