当我使用 npm 安装这样的 TypeScript 定义时
npm install --save @types/express
我无法使用已安装的模块,因为它们无法相互解决。例如,@types/express 需要 @types/express-static-server-core,但作为
@types/express/index.d.ts
包含到 express-static-server-core
的相对路径,模块无法解析:node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/@types/express/index.d.ts(17,23): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/@types/serve-static/index.d.ts(15,26): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.
我该如何解决这个问题?安装 TypeScript 定义的最佳方式是什么?
据我所知,不推荐使用类型,所以我尝试从@types 安装类型定义,然后使用
tsc --target ES5 --module commonjs index.ts
但它还没有工作。我究竟做错了什么?
最佳答案
碰巧这是相关的,在一个项目中,我将泛型类型与 express 一起使用:
import { ParamsDictionary, Request, ... } from 'express-serve-static-core';
// ...
app.get<ParamsDictionary, UserDetails>(...);
那行得通。当我尝试在另一个不使用泛型类型的项目中做同样的事情时:import { Request, Response } from 'express-serve-static-core';
我得到了'无法解析模块“express-serve-static-core ”的路径'。当我将导入更改为:
import express, { Request, Response } from 'express';
问题消失了。
关于node.js - TypeScript @types 模块无法相互解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820870/