【问题标题】:How to get TypeScript to treat any given folder the same way it treats the 'types' folder?如何让 TypeScript 以与处理“类型”文件夹相同的方式处理任何给定文件夹?
【发布时间】:2018-01-05 06:23:54
【问题描述】:

所以我为一个命名空间下的全局库编写了一堆声明文件。我有超过 50 个 d.ts 文件组织在各种子文件夹中,都在 types 文件夹中。所有的文件都是这样写的:

declare namespace pc {
  class Foo {}
  function bar(): void;
}

我没有在我的声明文件或项目的源代码中的任何地方使用任何三斜杠指令、导出或导入。 TypeScript 只是做它的事情,一切正常。现在假设我想将我的所有声明文件移动到另一个文件夹,比如文件夹 node_modules/playcanvas-typings,我需要做什么才能让 TypeScript 使用该文件夹中的声明文件,就像使用 @987654327 中的声明文件一样@文件夹?

我尝试将文件夹添加到tsconfig.json 中的typeRoots,但它不起作用。这是我tsconfig.json 中的编译器选项:

"compilerOptions": {
  /* Basic Options */
  "target": "es5",                          
  "module": "commonjs",                     
  "lib": [                                  
    "es2017",      
    "dom"
  ],     
  "sourceMap": true,                        
  /* Strict Type-Checking Options */
  "strict": true,
  /* Module Resolution Options */
  "moduleResolution": "node",               
  "typeRoots": [   
    "node_modules/playcanvas-typings"
  ],    
  /* Experimental Options */
  "experimentalDecorators": true,           
  "emitDecoratorMetadata": true             
}

我的 IDE 和编译器给了我Cannot find namespace 'pc' 错误。

【问题讨论】:

  • 把它放在tsconfig.json:typescriptlang.org/docs/handbook/…
  • @jonrsharpe我试过把它放在typeRoots,但它不起作用。
  • 然后给minimal reproducible example 说明您尝试了什么以及结果是什么(“不起作用” 对人们来说工作量并不大)。
  • @jonrsharpe 我已根据您的建议编辑了我的问题。如果我需要添加任何其他信息,请告诉我,并感谢您的帮助。

标签: typescript declaration


【解决方案1】:

将您的文件夹包含在tsconfig.json

{
  ...
  "include": [
    "types"
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多