创建一个 TypeScript 库并在 Node.js 中使用 ES6 和 TypeScript

Ale*_*ler 0 node.js jsdoc typescript

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

该库的目标是扩展Request类型express并提供额外的属性。

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

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "types": ["mocha"]
  }
}
Run Code Online (Sandbox Code Playgroud)

这些是 的相关部分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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

- dist
- src
  - http
  - security
- test
Run Code Online (Sandbox Code Playgroud)

我创建了一个新的打字稿文件AuthenticatedRequest.tssrc/http

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

export interface AuthenticatedRequest extends Request {
  user: UserReference
}
Run Code Online (Sandbox Code Playgroud)

src/security包含一个UserReference.ts

import {Claim} from "./Claim";

export interface UserReference {
  claims: Claim[];
}
Run Code Online (Sandbox Code Playgroud)

和一个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;
  }
}
Run Code Online (Sandbox Code Playgroud)

IClaim.ts 看起来像这样:

export interface IClaim {
  type: string,
  value: string
}
Run Code Online (Sandbox Code Playgroud)

在 中test,我创建了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();
  });
});
Run Code Online (Sandbox Code Playgroud)

现在我有几个问题:

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

我还创建了一个用于集成的本地测试项目,并通过npm link.

但不是使用

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

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

我怎样才能摆脱dist这里的文件夹名称?

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

在此处输入图片说明

Kar*_*ski 6

package.json

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

    "types": "./dist/index.d.ts"

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

运行测试

  • 这是解决此问题的预期 TypeScript 方法吗?

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

  • 是否可以只使用require("../dist/security/Claim");代替require("../dist/security/Claim").Claim;

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

    import Claim from "../dist/security/Claim";
    
    Run Code Online (Sandbox Code Playgroud)

    或者:

    const Claim = require("../dist/security/Claim");
    
    Run Code Online (Sandbox Code Playgroud)
  • 除了使用该jsdoc声明/** @type {AuthenticatedRequest} */我想用/** @type {myLib.http.AuthenticatedRequest} */

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

    /**
     * @type {import('path/to/AuthenticatedRequest')}
     */
    const req {}
    
    Run Code Online (Sandbox Code Playgroud)

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

    "dependencies": {
      "my-library": "../path/to/the/root"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 另外,AuthenticatedRequest在集成测试项目中使用 jsdoc 注释时,我收到找不到类型的错误:

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


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