Typescript typeRoots 未检测类型定义

Sul*_*ahi 5 node.js node-modules typescript tsconfig microservices

我在我的项目中使用 TypeScript。如果我尝试使用 commonJS 导入包

\n
const { v4: uuidv4 } = require(\'uuid\');\n
Run Code Online (Sandbox Code Playgroud)\n

然后我在编译期间不会收到任何错误,但是当我将其转换为 ES6 模块时,例如

\n
import { v4 as uuidv4 } from \'uuid\'; \n
Run Code Online (Sandbox Code Playgroud)\n

我收到如下编译错误:

\n
\n

找不到模块“uuid”的声明文件。\'C:/Users/project/dependencies/nodejs/node_modules/uuid/dist/index.js\' 隐式具有 \'any\' 类型。尝试\'npm i --save-dev @types/uuid\'(如果存在)或添加包含\'声明模块\'uuid\'的新声明(.d.ts)文件;

\n
\n

我已经安装了@types/uuid。

\n

我确实遇到了需要类型定义(@types/*)的包的问题

\n

如果项目结构类似于下面的示例:

\n
const { v4: uuidv4 } = require(\'uuid\');\n
Run Code Online (Sandbox Code Playgroud)\n

要重新生成此问题,请运行下面提到的命令:

\n
import { v4 as uuidv4 } from \'uuid\'; \n
Run Code Online (Sandbox Code Playgroud)\n

将以下 json 添加到 tsconfig.json 文件:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dependencies\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nodejs\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x82\xc2\xa0 \xc2\xa0    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n\xe2\x94\x82\xc2\xa0 \xc2\xa0    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0 \xc2\xa0\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.ts\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 .gitignore\n
Run Code Online (Sandbox Code Playgroud)\n
$ mkdir project\n$ cd project\n$ touch tsconfig.json\n
Run Code Online (Sandbox Code Playgroud)\n
import { v4 as uuid } from \'uuid\'; // inline typescript error (same mentioned in the issue)\nconsole.log(uuid()); \n
Run Code Online (Sandbox Code Playgroud)\n

uuid 确实支持 ESM 模块,我确实创建了一个问题。我终于发现,如果 package.json 存在于项目的根目录中,我将永远不必处理这样的问题。

\n

例子:

\n
{\n  "compilerOptions": {\n    "target": "es2017",                       \n    "module": "commonjs",                     \n    "outDir": "./dist",\n    "strict": true,\n    "noImplicitAny": true,\n        "baseUrl": "./dependencies/nodejs/node_modules",\n    "typeRoots": ["./dependencies/nodejs/node_modules/@types"],\n    "esModuleInterop": true,\n    "inlineSourceMap": true,\n    "skipLibCheck": true,\n    "forceConsistentCasingInFileNames": true\n  },\n  "include": ["src/**/*"],\n  "exclude": ["dependencies", "**/*.spec.ts"]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当使用嵌套结构化项目(其中 package.json 文件在根目录中不可用)时,我仅遇到需要类型定义(@types/*)的包的问题。

\n

该项目结构基于使用 AWS Lambda 和无服务器应用程序层。这个“依赖项”文件夹基本上是一个层,AWS-lambda 层的定义结构如下

\n
\n

依赖项/nodejs/package.json

\n
\n

因此,package.json 不应放置在项目的根目录下。

\n

但是,我相信这不是包问题,而是打字稿 TSconfig 问题。

\n

我尝试在 tsconfig 文件中定义 typeRoots 但它不起作用。

\n

以下是 github 存储库,其中包含生成问题的示例项目:

\n
\n

https://github.com/sulemanelahi/typescript-type-definition

\n
\n

我想使用 ES6 模块在嵌套结构项目中导入 uuid,与我共享的相同。

\n

And*_*len 5

以下对我有用

import { v4 as uuidv4 }  from 'uuid/index';
Run Code Online (Sandbox Code Playgroud)

推理:

跑步

tsc --traceResolution
Run Code Online (Sandbox Code Playgroud)

有痕迹

File '.../dependencies/nodejs/node_modules/@types/uuid.ts' does not exist.
File '.../dependencies/nodejs/node_modules/@types/uuid.tsx' does not exist.
File '.../dependencies/nodejs/node_modules/@types/uuid.d.ts' does not exist.
Run Code Online (Sandbox Code Playgroud)

扩展这个逻辑

"typeRoots": ["dependencies/nodejs/node_modules/@types/**/index.d.ts"],
Run Code Online (Sandbox Code Playgroud)

允许返回到

import { v4 as uuidv4 }  from 'uuid';
Run Code Online (Sandbox Code Playgroud)

更新:我也设置了

"baseUrl": "./dependencies/nodejs/node_modules/@types"
Run Code Online (Sandbox Code Playgroud)

这使得

"typeRoots": ["**/*.d.ts"],
Run Code Online (Sandbox Code Playgroud)


Sul*_*ahi 0

我可以通过添加 paths 属性来解决该问题:

"baseUrl": "./dependencies/nodejs/node_modules",    
"paths": {
  "uuid": ["./@types/uuid"]
}
Run Code Online (Sandbox Code Playgroud)

TypeRoots 定义全局类型的路径。这基本上表明 .d.ts 文件包含声明模块语法,而像 @types/UUID 这样的包(类型定义)则不包含。

更新:我使用了@Andrew Allen的解决方案,他在他的答案下面的评论部分提到了他从这个github评论中得到的解决方案(安德鲁没有在答案中更新它)。

这也解决了问题,而且最好编写每个类型定义包路径。

{
    "compilerOptions": {
        "target": "es5"
        "baseUrl": "./dependencies/nodejs/node_modules",
        "paths": {
            "*" : ["*"]
        }
    }
}`
Run Code Online (Sandbox Code Playgroud)