【问题标题】:tsconfig to enable intellisense in vscode for node 14tsconfig 在节点 14 的 vscode 中启用智能感知
【发布时间】:2021-06-27 16:16:44
【问题描述】:

我在我维护的 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" 的普通节点 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 或者导出默认值是否可能是问题所在。任何帮助将不胜感激。非常感谢!

【问题讨论】:

    标签: node.js typescript visual-studio-code


    【解决方案1】:

    在将 typescript 包发布到 npm 时,请确认您已添加以下内容。如果您错过了上述任何一点,请添加并测试已发布的智能感知包:

    • "declaration": true 添加到您的tsconfig.json。这告诉 TypeScript 发出一个 .d.ts 定义文件以及你编译的 JavaScript。
    • 指出package.json中的主文件和类型文件(你的.d.ts文件的文件名将与你的主入口点相同。),像这样(假设你在tsconfig中的outDir是一个文件夹命名为dist):
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    
    • 在 package.json 中添加 prepublish 键,以便在发布前使用 tsc 进行编译:
    "prepublish": "tsc"
    

    【讨论】:

    • 非常感谢! package.json 中缺少 dist/index.d.ts。成功了!
    猜你喜欢
    • 2019-11-11
    • 1970-01-01
    • 2016-11-23
    • 2016-08-22
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多