如何初始化一个typescript项目

摘要:本文将展示如何从0开始初始化一个 typescript 项目。首先,我们选定一个文件夹,然后在文件夹中执行 npm init -y 命令来对项目进行初始化。

本文将展示如何从0开始初始化一个 typescript 项目。

初始化

首先,我们选定一个文件夹,然后在文件夹中执行 npm init -y 命令来对项目进行初始化。

anjie@panjies-Mac-Pro typescript-init % npm init -y
Wrote to /Users/panjie/github/yunzhiclub/typescript-init/package.json:

{
  "name": "typescript-init",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/yunzhiclub/typescript-init.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/yunzhiclub/typescript-init/issues"
  },
  "homepage": "https://github.com/yunzhiclub/typescript-init#readme"
}

该命令将为我们生成一个 package.json 文件:

panjie@panjies-Mac-Pro typescript-init % tree
.
├── README.md
└── package.json

0 directories, 2 files

安装typescript

接下来,我们使用 npm i typescript --save-dev 来安装 ts :

panjie@panjies-Mac-Pro typescript-init % npm i typescript --save-dev
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN typescript-init@1.0.0 No description

+ typescript@4.6.4
added 1 package from 1 contributor and audited 1 package in 2.22s
found 0 vulnerabilities

typescript初始化

ts安装后,我们也需要一个初始化操作,该操作将默认生成ts的配置文件,对应的命令为 npx tsc --init

panjie@panjies-Mac-Pro typescript-init % npx tsc --init

Created a new tsconfig.json with:                                               
                                                                             TS 
  target: es2016
  module: commonjs
  strict: true
  esModuleInterop: true
  skipLibCheck: true
  forceConsistentCasingInFileNames: true


You can learn more at https://aka.ms/tsconfig.json

该命令将为我们自动生成 tsconfig.json ,如果你想进行一些定制,则只需要打开此文件,找到对应的项进行修改或启用即可。

index.ts

基本的初始化工作完成后,便可以创建 index.ts ,并进行代码的测试了。

'use strict';

const hello = (world: string) => {
  console.log(`hello ${world}`);
}

hello('world');

编译运行

文件创建完成后进行编译及运行:

panjie@panjies-Mac-Pro typescript-init % npx tsc index.ts
panjie@panjies-Mac-Pro typescript-init % node index.js
hello world

自动编译运行

每次变更文件化都手动执行一下编译及运行固然可行,但这种方式的确无法忍受。tsc-watch则专门为此而生,运行 npm install tsc-watch --save-dev 来安装 tsc-watch :

panjie@panjies-Mac-Pro typescript-init % npm install tsc-watch --save-dev
npm WARN typescript-init@1.0.0 No description

+ tsc-watch@5.0.3
added 20 packages from 16 contributors and audited 21 packages in 3.788s
found 0 vulnerabilities

安装完成后,我们打开 package.json 文件,在 scripts 中增加以下 dev 项:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "tsc-watch --noClear -p ./tsconfig.json --onSuccess \"node ./index.js\""
  },

然后我们在命令行中运行 npm run dev 则可以实现:当文件变更时重新编译、重新运行的目的。

本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_11441