简体   繁体   English

如果不在/ node_modules / @ types / *文件夹中,则不考虑DefinedTypes文件

[英]DefinedTypes file not taken into account when not in the /node_modules/@types/* folder

It seems that my typescript compiler is not detecting the DefinedTypes file of the modules when they are in the module (./node_modules//index.d.ts) but detects them in @types folder (./node_modules/@types//index.d.ts). 看来我的TypeScript编译器未在模块(./node_modules//index.d.ts)中检测模块的DefinedTypes文件,而是在@types文件夹(./node_modules/@types//index)中检测到它们。 .d.ts)。

For example with express-validator, the module comes with its defined type (see: https://github.com/milkeg/testDefinedType/blob/master/node_modules/express-validator/index.d.ts ) but when I try to compile using tsc command, I have the following error: 例如,对于express-validator,模块带有其定义的类型(请参阅: https : //github.com/milkeg/testDefinedType/blob/master/node_modules/express-validator/index.d.ts ),但是当我尝试使用tsc命令编译时,出现以下错误:

app.ts:5:28 - error TS2307: Cannot find module 'express-validator'.

5 import * as validator from 'express-validator';

My tsconfig.json file is: 我的tsconfig.json文件是:

{
    "compilerOptions": {
      "outDir": "./build",
      "allowJs": true,
      "target": "es2018",
      "alwaysStrict": true,
    },
    "include": [
      "./**/*",
    ],
    "exclude": [
      "./node_modules/**/*",
      "./build/**/*"
    ],
}

I tried to add "./node_modules/**/*.d.ts" to the include option, but it does not help. 我试图将“ ./node_modules/**/*.d.ts”添加到include选项,但这无济于事。

Repo of the example: https://github.com/milkeg/testDefinedType 示例的仓库: https : //github.com/milkeg/testDefinedType

What should I change to make sure that tsc is taking into account the .d.ts file that are within the module itself (and not only under ./node_modules/@types/*)? 为确保tsc考虑模块本身内的.d.ts文件(并且不仅限于./node_modules/@types/*下),我应该进行哪些更改?

Just add 只需添加

"typeRoots": [
  "./node_modules/@types",
  "./path/to/custom/dtsfolder/"
],

to your tsconfig.json . tsconfig.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM