Typescript - 基础(一)

2020-05-03  本文已影响0人  酷热summer

1、强类型与弱类型

2、动态类型语言和静态类型语言

// c++
class C {
  public:
    int x;
    int y;
}
int add (C a, C b) {
  return a.x + a.y + b.x + b.y;
}
// javascript
class C {
  constructor(x,y) {
    this.x = x;
    this.y = y;
  }
}
function add(a,b) {
  return a.x + a.y + b.x + b.y;
}

3、安装和使用

在项目目录下 npm init -y,然后全局安装 typescript:npm i typescript -g,安装完成后可以直接使用 tsc 命令。可通过 tsc -h 查看 typescript 的指令。在项目目录下输入 tsc --init 可以初始化 typescript 配置项。此时,项目目录下同时生成 tsconfig.json 文件。

在根目录创建index.ts文件,内容为const str:string = 'Hello World!';,然后在终端输入 tsc ./index.js,会在 index.ts 目录下生成 index.js 文件。

上一篇下一篇

猜你喜欢

热点阅读