关于typescript:从零开始创建TypeScript项目

1次阅读

共计 1150 个字符,预计需要花费 3 分钟才能阅读完成。

1、创立文件夹

间接创立 typescipt 文件夹
通过终端创立 typescript 文件夹
mkdir typescript
cd typescript

2、进入 typescript 文件夹,初始化 npm

npm init

依据提醒回车,这时候文件夹中会生成 package.json 文件。

3、装置 typescript/tslint 和 NodeJS 的类型申明

npm install --save-dev typescript tslint @types/node

tslint 是代码规定查看工具,放弃代码格调统一。这时候文件夹中就会生成 node_modules 文件夹

4、配置 tsconfig.json

{
    "compilerOptions": {"lib": ["ES2015"],
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true,
        "strict": true,
        "target": "es2015"
    },
    "include": ["src"]
}

能够本人新建并配置,或者应用命令初始化,处于 typescript 目录下,运行以下命令

./node_modules/.bin/tsc --init
 根本配置我的项目阐明:include ts 文件在哪个文件夹中
lib     运行环境包含哪些 api
module  ts 代码编译成什么模块零碎
outDir  编译后的 js 代码寄存地位
stirct  是否开启严格模式
target  ts 编译成 js 的规范
... 有很多配置项具体能够查看其它材料 

5、配置 tslint.json

{
    "defaultSeverity": "error",
    "extends": ["tslint:recommended"],
    "jsRules": {},
    "rules": {"no-console": false},
    "rulesDirectory": []}

能够本人新建并配置或者应用命令初始化,处于 typescript 目录下,运行以下命令

./node_modules/.bin/tslint --init

6、在 src 目录中创立 index.ts 文件并输出内容

mkdir src
touch src/index.ts

当初整体目录如下
typescript
----node_modules/
----src/
    ----index.ts
----package.json
----tsconfig.json
----tslint.json
index.ts
...
console.log("hello TypeScript")

7、编译

./node_modules/.bin/tsc

这时候文件夹中就会生成 dist/index.js

8、运行

node ./dist/index.js

如果中断输入“hello Typescript” 那么阐明咱们第一个 typescript 我的项目配置胜利

正文完
 0