node.js - 创建一个 TypeScript 库并通过 ES6 和 TypeScript 从 Node.js 中使用它

标签 node.js typescript jsdoc

我想创建一个 TypeScript 库作为私有(private) npm 包,可以在 Node.js(包括 6.x)中使用,使用支持 @types 的 ES6 和 TypeScript .

该库的目标是从 express 扩展 Request 类型并提供其他属性。

我创建了一个新的 Node.js 项目并添加了这个 tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "types": ["mocha"]
  }
}

这些是package.json的相关部分:

{
  "name": "@myscope/my-lib",
  "main": "dist",
  "scripts": {
    "build": "rm -rf ./dist && ./node_modules/.bin/tsc",
    "test": "./node_modules/.bin/mocha test"
  },
  "private": true,
  "dependencies": {
    "joi": "11.4.0"
  },
  "devDependencies":  {
    "mocha": "^5.2.0",
    "express": "^4.16.4",
    "@types/express": "^4.16.1",
    "@types/joi": "^14.3.0",
    "@types/mocha": "^5.2.5",
    "typescript": "^3.2.4"
  }
}

我的文件夹结构是这样的:

- dist
- src
  - http
  - security
- test

我在 src/http 中创建了一个新的 TypeScript 文件 AuthenticatedRequest.ts:

import {Request} from "express";
import {UserReference} from "../security/UserReference";

export interface AuthenticatedRequest extends Request {
  user: UserReference
}

src/security 包含一个 UserReference.ts:

import {Claim} from "./Claim";

export interface UserReference {
  claims: Claim[];
}

和一个Claim.ts:

import {IClaim} from "./IClaim";

export class Claim implements IClaim {
  type: string;
  value: string;

  constructor(type: string, value: string) {
    this.type = type;
    this.value = value;
  }
}

IClaim.ts 如下所示:

export interface IClaim {
  type: string,
  value: string
}

测试中,我创建了AuthenticatedRequestTests.js(纯ES6,这里没有TypeScript来验证ES6的代码完成和使用):

'use strict';

const assert = require('assert');
const Claim = require("../dist/security/Claim").Claim;

describe('req', () => {
  it('should ', done => {
    /** @type {AuthenticatedRequest} */
    const req = {};
    req.user = { claims: [new Claim('tenantId', '123')] };
    assert.equal(req.user.claims[ 0 ].type, 'tenantId');
    assert.equal(req.user.claims[ 0 ].value, '123');
    return done();
  });
});

现在我有几个问题:

  • 这是解决此问题的预期 TypeScript 方法吗?
  • 是否可以只使用 require("../dist/security/Claim"); 而不是 require("../dist/security/Claim").Claim ;
  • 我不想使用这个 jsdoc 语句 /** @type {AuthenticatedRequest} */ 我想使用 /** @type {myLib.http .AuthenticatedRequest} */

我还创建了一个用于集成的本地测试项目,并通过 npm 链接 安装了我的库。

但不要使用

const Claim = require("@scope/my-lib/security/Claim").Claim; 我必须使用

const Claim = require("@scope/my-lib/dist/security/Claim").Claim;

如何去掉此处的 dist 文件夹名称?

此外,在集成测试项目中使用 AuthenticatedRequestjsdoc 注释,我收到找不到类型的错误:

enter image description here

最佳答案

package.json

  • 应该有一个名为types(或typings)的字段,告诉您的库使用者您的项目的类型定义在哪里。如果它们是由 TypeScript 生成并保存到 dist/index.d.ts,那么这就是应该使用的路径。

    “类型”:“./dist/index.d.ts”

  • 应该有一个名为 files 的字段,其中包含将交付给最终用户的文件/目录数组。

运行测试

  • Is this the expected TypeScript way to solve this?

    如果您使用 TypeScript 来开发库,则没有理由不使用 TypeScript 进行测试。那里有兼容 TypeScript 的测试运行程序(ts-jest 曾经很流行,现在 Jest 能够立即理解 TypeScript)。

  • Is it possible to just use require("../dist/security/Claim"); instead of require("../dist/security/Claim").Claim;?

    使用 TypeScript,可以使用几种语法。您可以使用默认导出来导出 Claim 并执行以下操作:

    import Claim from "../dist/security/Claim";
    

    或者:

    const Claim = require("../dist/security/Claim");
    
  • Instead of using this jsdoc statement /** @type {AuthenticatedRequest} */ I would like to use /** @type {myLib.http.AuthenticatedRequest} */.

    您将需要一个导入类型。它们看起来像这样:

    /**
     * @type {import('path/to/AuthenticatedRequest')}
     */
    const req {}
    

    路径可以是相对路径或绝对路径。如果您希望将本地代码库视为是从 npm 安装的,则可以在测试目录中创建另一个 package.json 文件,并使用相对路径来解析您的库模块。

    "dependencies": {
      "my-library": "../path/to/the/root"
    }
    
  • Also, using the jsdoc comment for AuthenticatedRequest in the integration test project, I get the error that the type cannot be found:

    这也可以通过导入类型来解决。除非类型不在全局范围内,否则您需要先导入它,然后才能使用它。请改用 import('path/to/AuthenticatedRequest')

<小时/>

有一些事情正在发生。如果您可以创建一个公共(public)存储库来展示您的问题,我相信我们会更容易帮助您解决这些问题。

关于node.js - 创建一个 TypeScript 库并通过 ES6 和 TypeScript 从 Node.js 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54357435/

相关文章:

javascript - 从对象集合中的项目的 lodash.find 方法获取未定义

node.js - 如何在 Express.js 上托管 jsdoc/apidoc

javascript - 如何仅使用 jsdoc 在 webstorm 中记录类型?

node.js - 使用 Node JS 在哪里存储 CMS 图像?

arrays - 两个数组之间的 typescript 差异

node.js - Nodejs 在路由器页面中包含 socket.io

javascript - 仅第一个添加值显示在 ng2-chart 折线图中

javascript - JSDoc 中的文档重载函数

node.js - http.request 的响应如何在两个可写流的幕后工作?

javascript - Gatsby 开发命令运行但 gatsby 构建出现错误