我维护的 TypeScript npm 包中有以下 tsconfig.json。
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": ["es2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["scripts/", "test/"]
}
我通过 index.ts 中的默认导出导出所有函数、类和实用程序
export default {
CoolClass,
CoolClass2,
utils: {
utilFunction,
utilFunction2,
}
}
我在没有 "type": "module"
的普通 node v14 项目中使用 npm 包。我启用了 eslint。对于我使用的 npm 包,我通常会得到很好的智能感知。
我不知道为什么,但是对于从该包编译的 TypeScript,我没有得到任何智能感知。
const { default as package } = require('mypackage');
const myClass = new package.CoolClass()
// if I hover over CoolClass, it shows (property) SpeechResponse: an
myClass. // if I hover over my class, it shows const resp: any
此外,类型化的对象参数会失去智能感知,这很烦人。
package.utils.utilFunction({ typedProperty: ... })
// I don't get any autocompletion for possible properties
我不确定是否需要更改我的 tsconfig.json 或者导出默认值是否可能是问题所在。任何帮助将不胜感激。非常感谢!
答案 0 :(得分:1)
在将您的打字稿包发布到 npm 时,请确认您已添加以下内容。如果您遗漏了上述任何一点,请添加并测试已发布的智能感知包:
"declaration": true
添加到您的 tsconfig.json
。这告诉 TypeScript 随编译的 JavaScript 一起发出 .d.ts 定义文件。package.json
中指出主文件和类型文件(您的 .d.ts 文件的文件名将与您的主入口点相同。),就像这样(假设您的 outDir
在tsconfig 是一个名为 dist
) 的文件夹:"main": "dist/index.js",
"types": "dist/index.d.ts",
"prepublish": "tsc"