【问题标题】:How to disable excess auto import suggestions in VS Code / TypeScript?如何在 VS Code / TypeScript 中禁用多余的自动导入建议?
【发布时间】:2019-06-10 14:13:44
【问题描述】:

我有一个带有类型的 npm 包,文件结构类似于以下:

./typings/index.d.ts:

export { ISomeInterface } from './something';

./typings/something.d.ts:

/*
  This interface is used by the package internally,
  so I can't simply remove the export.
*/
export interface ISomeOtherInterface {
  someField: number;
}

export interface ISomeInterface {
  someObject: ISomeOtherInterface;
}

./package.json:

"typings": "./typings/index.d.ts"

问题是每当我输入类似的东西时

const a: ISome...

VS Code 的 IntelliSense 向我展示了两个建议:

| Auto import ISomeInterface from 'my-package'
| Auto import ISomeOtherInterface from 'my-package/typings/something'

防止 VS Code 自动提示第二次导入的正确方法是什么?

typescript@3.2.2, VSCode@1.30.2

更新:

library tsconfig(用于为库生成类型的那个):

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "esnext",                     
    "lib": ["dom", "es2016", "esnext"],                             
    "jsx": "react",  
    "importHelpers": true,
    "strict": true,                           
    "noImplicitAny": true,                 
    "strictNullChecks": true,              
    "strictFunctionTypes": true,           
    "strictBindCallApply": true,           
    "strictPropertyInitialization": true,  
    "noImplicitThis": true,                
    "alwaysStrict": true,                  
    "noUnusedLocals": true,                
    "noImplicitReturns": true,             
    "noFallthroughCasesInSwitch": true,    
    "moduleResolution": "node",            
    "allowSyntheticDefaultImports": true,  
    "esModuleInterop": true,
    "declaration": true,
    "emitDeclarationOnly": true,
    "rootDir": "src",
    "outDir": "typings"                   
  },
  "include": [
    "src/lib/**/*.ts",
    "src/lib/**/*.tsx"
  ],
  "exclude": [
    "node_modules",
    "dist",
    "typings",
    "src/**/*.spec.ts",
    "src/**/*.spec.tsx"
  ]
}

project tsconfig(使用库的项目中使用的那个):

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "esnext",                     
    "lib": ["dom", "es2016", "esnext"],                             
    "jsx": "react",
    "importHelpers": true,
    "strict": true,                           
    "noImplicitAny": true,                 
    "strictNullChecks": true,              
    "strictFunctionTypes": true,           
    "strictBindCallApply": true,           
    "alwaysStrict": true,
    "noUnusedLocals": true,              
    "noImplicitReturns": true,             
    "noFallthroughCasesInSwitch": true,    
    "moduleResolution": "node",            
    "baseUrl": "./",
    "allowSyntheticDefaultImports": true,  
    "esModuleInterop": true,
    "paths": {
      "*": ["node_modules/@types/*", "*"]
    }
  }
}

【问题讨论】:

  • 你能和我们分享你的 tsconfig.json 吗?
  • @Vincenzo 当然,原帖现在包含两个配置

标签: typescript visual-studio-code typescript-typings


【解决方案1】:

我能想到的两件事。

  1. 在第一个 tsconfig 中,尝试将 outFile 设置为 index.d.ts。它应该只生成 1 个声明文件。

  2. 尝试从第二个 tsconfig 中删除它

"paths": {
  "*": ["node_modules/@types/*", "*"]
}

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2021-02-17
    • 1970-01-01
    • 2021-11-16
    • 2019-06-27
    • 1970-01-01
    • 2018-07-11
    • 2021-03-04
    相关资源
    最近更新 更多