1. 安装依赖
λ yarn add typescript types/node concurrently nodemon wait-on -D
  1. 初始化一个 tsconfig.json
λ ./node_modules/.bin/tsc --init
  1. 编写 tsconfig.json
{
"compilerOptions": {
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
"rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"baseUrl": "./",
"removeComments": true /* 不要向输出发出注释。 */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"experimentalDecorators": true /* 为ES7装饰器提供实验支持。 */,
"emitDecoratorMetadata": true /* 装饰器支持. */
},
"exclude": ["node_modules"]
}
  1. 创建 tsconfig.build.json
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "dist"]
}
  1. 创建 nodemon.json
{
"watch": ["dist"],
"ext": "js",
"exec": "node dist/index"
}
  1. package.json
{
"scripts": {
"start": "concurrently --handle-input \"wait-on ./dist/index.js && nodemon\" \"tsc -w -p tsconfig.build.json\" "
},
"devDependencies": {
"@types/node": "^12.0.7",
"typescript": "^3.5.1",
"wait-on": "^3.2.0",
"concurrently": "^4.1.0",
"nodemon": "^1.19.1"
}
}

执行命令

λ npm start

添加测试 更多 github

  • 安装jest的vscode插件也行
  1. 安装依赖
λ npm i -D @types/jest jest ts-jest ts-node
  1. 增加scripts
"scripts": {
...
"test": "jest",
"coverage": "jest --coverage"
}
  1. 设置VScode的launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${relativeFile}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}
  1. 设置配置文件jest.config.js
module.exports = {
transform: { "^.+\\.ts?$": "ts-jest" },
testEnvironment: "node",
testRegex: "/test/.*\\.(test|spec)?\\.(ts|tsx)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};
  1. 添加测试脚本 /test/test.test.ts
it("tobe", () => {
expect("asd").toBe("asd");
});
  1. 运行测试
npm t

关于 ts-node

可以单独的运行ts文件,而无需编译

λ npm i -g ts-node
λ ts-node index.ts
hello ts

Nodejs 使用 TypeScript的更多相关文章

  1. nodejs + koa + typescript 集成和自动重启

    版本说明 Node.js: 16.13.1 全局安装 TypeScript yarn global add typescript 创建项目 创建如下目录结构 project ├── src │ └── ...

  2. TypeScript Basic Types(基本类型)

    在学习TypeScript之前,我们需要先知道怎么才能让TypeScript写的东西正确的运行起来.有两种方式:使用Visual studio 和使用 NodeJs. 这里我选择的是NodeJs来编译 ...

  3. nodejs+express blog项目分享

    项目简介:项目采用nodejs+express+typescript+mongodb技术搭建 主要功能: 1.用户注册 2.用户登录 3.文章管理模块 4.图片管理模块 5.token认证 6.密码加 ...

  4. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...

  5. typescript + vue开发遇到的坑

    1,错误 :TS2304: Cannot find name 'require' 在ts使用nodejs,没有安装nodejs的TypeScript定义类型 ,使用require报的错 解决方法:如果 ...

  6. 拥抱开源,Office 365开发迎来新时代

    前言 作为全球最大的开放源代码托管平台,Github在上周迎来了它的十岁生日.自从2008年正式上线以来,Github上面汇聚了数以千万计的开发人员和各种项目,它几乎成为了开源的代名词和风向标,各大软 ...

  7. JEECG前后端分离UI框架实战版本抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)

    JEECG前后端分离UI框架实战版本 - 抢先体验 (ng2-admin+Angular4+AdminLTE) 关键词: ng2-admin.Angular4.AdminLTE.Nodejs.Jeec ...

  8. JEECG前后端分离UI框架实战抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)

    JEECG前后端分离UI框架 (ng2-admin+Angular4+AdminLTE) 关键词: ng2-admin.Angular4.AdminLTE.Nodejs.Jeecg JEECG紧跟技术 ...

  9. graphql 后台服务项目架构(一)

    基础知识 简而言之,GraphQL 是一种描述如何请求数据的语法,通常用于客户端向服务器请求数据.GraphQL 有三个主要特点: 允许客户端精确指定所需数据. 可以更容易地从多个数据源聚合数据. 使 ...

随机推荐

  1. (六)整合 QuartJob ,实现定时器实时管理

    整合 QuartJob ,实现定时器实时管理 1.QuartJob简介 1.1 核心API 2.SpringBoot整合QuartJob 2.1 项目结构 2.2 定时器配置 2.3 定时器管理工具 ...

  2. Spark 将DataFrame所有的列类型改为double

    Spark 将DataFrame所有的列类型改为double 1.单列转化方法 2.循环转变 3.通过:_* 1.单列转化方法 import org.apache.spark.sql.types._ ...

  3. python函数的实例,书写一个创建有针对性的专用密码字典的程序

    python学习,实战学习,函数的学习与使用,综合知识的运用.包括for ,while循环,if...else.. 和if... elif ... else 的条件判断! 问题描述:书写一个创建有针对 ...

  4. svn安装步骤

    我使用的是myeclipse 8.5  svn是site-1.8.22.zip 步骤 1.在myeclipse安装路径下dropins文件夹中创建svn文件夹 2.解压site-1.8.22.zip复 ...

  5. js文字颜色闪烁

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 小白搭建WAMP详细教程---apache安装与设置

    一.apache官网下载Apache HTTP Server服务器 1.打开apache官网http://www.apache.org/,拉到最底下,找到HTTP Server,点击进去下载.也可以直 ...

  7. xLua热更新插件

    一.xLua插件下载安装 1.从GitHub上搜索并下载插件 2.将文件复制到unity中 3.检查是否有错误 二.在unity中调用lua 1.简单调用 在c#脚本中使用LuaEnv类可以运行lua ...

  8. 设计模式(十)——组合模式(HashMap源码解析)

    1 看一个学校院系展示需求 编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院, 一个学院有多个系.如图: 2 传统方案解决学校院系展示 3 传统方案解决 ...

  9. 翻译:《实用的Python编程》01_01_Python

    目录 | 下一节 (1.2 第一个程序) 1.1 Python Python 是什么? Python 是一种解释型(译者注:区别于编译型)的高级语言, 通常被归类为 "脚本语言"  ...

  10. Checkout Assistant CodeForces - 19B

    题意: 给你n个物品,每个物品有一个价格ci和一个支付时间ti,在这个ti时间内,你可以免费拿ti个物品.问你想要带走这n个物品最小需要多少钱 题解: 原本还想着贪心去写,但是好像贪心写不了,,,不属 ...