代码之家  ›  专栏  ›  技术社区  ›  Alexander Zeitler

创建一个TypeScript库,并将其从Node.js与ES6和TypeScript一起使用

  •  0
  • Alexander Zeitler  · 技术社区  · 6 年前

    6.x) 将ES6与 @types 支持和键入脚本。

    图书馆的目标是扩展 Request 输入自 express

    我创建了一个新的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
    

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

    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
    }
    

    在里面 test ,我创造了 AuthenticatedRequestTests.js (纯ES6,此处没有用于验证代码完成和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();
      });
    });
    

    • 是否可以只使用 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

    此外,使用 AuthenticatedRequest 在集成测试项目中,我发现找不到类型的错误:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  6
  •   Karol Majewski    6 年前

    package.json

    • 应该有一个名为 types (或 typings dist/index.d.ts

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

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

    运行测试

    • 如果您使用TypeScript开发库,那么没有理由不在测试中使用TypeScript。有符合TypeScript的测试运行程序( ts-jest

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

      使用TypeScript,可以使用几种语法。你可以出口 Claim

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

      或:

      const Claim = require("../dist/security/Claim");
      
    • 而不是使用这个jsdoc语句 /** @type {AuthenticatedRequest} */ 我想用 /** @type {myLib.http.AuthenticatedRequest} */ .

      导入类型 . 它们看起来是这样的:

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

      路径可以是相对的,也可以是绝对的。如果您希望将本地代码库视为是从npm安装的,那么可以创建另一个代码库 文件,并使用相对路径解析库模块。

      "dependencies": {
        "my-library": "../path/to/the/root"
      }
      
    • AuthenticatedRequest 在集成测试项目中,我发现找不到类型的错误:

      import('path/to/AuthenticatedRequest')


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

        2
  •  1
  •   Community CDub    4 年前

    我对你问题的一部分有另一个答案。你问

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

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

    我怎样才能去掉这里的dist文件夹名?

    正如卡罗尔指出的,你可以使用 files package.json 因此,在发布库时,您可以发送 dist 目录作为包根目录( plus package.json , README, etc github: 相反,NPM不会起作用,也不会起作用 npm link 地方发展。

    the exports property 相反:

    {
      "exports": {
        "./": "./dist/"
      }
    }
    

    现在 require("my_lib/security/Claim") 决心 node_modules/my_lib/dist/security/Claim baseUrl paths 在你的 tsconfig.json ,至少现在是这样。