node.js - TypeScript @types 模块无法相互解析

标签 node.js typescript typescript2.0

当我使用 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/

相关文章:

mysql - Ember 在组件目录中找不到模块 'mysql'

javascript - React Router - 未捕获的语法错误 : Unexpected token < when refreshing an URL with params

typescript - 覆盖抽象方法 TypeScript?

javascript - JQueryStatic 类型上不存在属性

node.js - 将 Node/Mongo 部署到 Openshift

node.js - 如何在数据库调用之外的 mongoIds 数组中找到 mongoId

javascript - 如何使用 JSON.parse 反序列化嵌套缓冲区

typescript - Angular 没有 NameService 的提供者

typescript - 有没有办法在 typescript 中自动生成 Azure Functions 的 swagger 文档?

angular - 与客户类型一样