【问题标题】:Re-export third party definitions in typescript npm module在 typescript npm 模块中重新导出第三方定义
【发布时间】:2018-11-02 16:41:12
【问题描述】:

我们有一个基于 pino 并使用 typescript 的私有 npm 日志记录模块。我们正在编译并发布到 npm。将模块导入应用程序时,我们收到错误:

node_modules/@scope/logging/lib/index.d.ts(1,23): error TS2688: Cannot find type definition file for 'pino'
node_modules/@scope/logging/lib/index.d.ts(2,23): error TS7016: Could not find a declaration file for module 'pino'. 'C:/app/node_modules/pino/pino.js' implicitly has an 'any' type. Try `npm install @types/pino` if it exists or add a new declaration (.d.ts) file containing `declare module 'pino';`

package.json

{
  "name": "@scope/logging",
  "version": "1.0.0",
  "main": "lib/index.js",
  "typings": "lib/index.d.ts",
  "dependencies": {
    "pino": "4.16.1"
  },
  "devDependencies": {
    "@types/pino": "4.7.1",
    "typescript": "2.8.3"
  }
}

tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "inlineSources": true,
    "inlineSourceMap": true,
    "declaration": true,
    "outDir": "lib",
    "baseUrl": ".",
    "typeRoots": ["node_modules/@types"],
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    },
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true
  },
  "typeAcquisition": {
    "enable": true
  },
  "include": ["src/**/*"],
  "exclude": [
    "**/*.spec.ts",
    "node_modules",
    "src/**/node_modules"
  ]
}

lib/index.ts

import * as pino from 'pino';

const isProduction = process.env.NODE_ENV === 'production';
const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL.toLowerCase() : (isProduction ? 'warn' : 'debug');

const logOpts: pino.LoggerOptions = {
  safe: true,
  level: logLevel,
  prettyPrint: !!isProduction,
};

export const logger = (category: string): pino.Logger => {
  return pino({
    name: category,
    ...logOpts,
  });
};

tsc编译后,输出文件如下:

lib/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const pino = require("pino");
const isProduction = process.env.NODE_ENV === 'production';
const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL.toLowerCase() : (isProduction ? 'warn' : 'debug');
const logOpts = {
    safe: true,
    level: logLevel,
    prettyPrint: !!isProduction,
};
exports.logger = (category) => {
    return pino(Object.assign({ name: category }, logOpts));
};

lib/index.d.ts

/// <reference types="pino" />
import * as pino from 'pino';
export declare const logger: (category: string) => pino.Logger;

现在发布后,我需要这个模块作为依赖并导入它:

app/index.ts

import { logger } from '@scope/logging';
const log = logger('Application');

log.info('Working');

这个错误让我相信我需要在编译时在我的模块中包含@types/pino/index.d.ts,但我不知道该怎么做。

【问题讨论】:

    标签: typescript npm


    【解决方案1】:

    我认为答案是将 @types/pino 作为 dependency 包含在模块的 package.json 中,因此在应用程序中使用时它与日志记录模块一起安装。将类型包含为依赖项感觉很奇怪,但它必须自动安装,而 peerDependency 不会这样做。

    【讨论】:

    • 如果依赖项没有发布的@types/pino 包而您只想使用本地定义的声明怎么办?
    • 您可以制作自己的 .d.ts 文件并将其导出到您的 npm 模块中
    猜你喜欢
    • 2017-04-14
    • 2021-08-25
    • 1970-01-01
    • 2023-04-01
    • 2020-06-27
    • 2016-08-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多