gpt4 book ai didi

typescript - 在 typescript npm 模块中重新导出第三方定义

转载 作者:行者123 更新时间:2023-12-04 13:02:58 24 4
gpt4 key购买 nike

我们有一个基于 pino 的私有(private) npm 日志模块并使用 typescript 。我们正在编译并发布到 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';`

包.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"
]
}

库/索引.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 编译后,这里是输出文件:

库/索引.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));
};

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

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

应用程序/index.ts
import { logger } from '@scope/logging';
const log = logger('Application');

log.info('Working');

该错误使我相信我需要包含 @types/pino/index.d.ts编译时在我的模块中,但我不知道如何做到这一点。

最佳答案

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

关于typescript - 在 typescript npm 模块中重新导出第三方定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com