【问题标题】:TypeScript @types modules fail to resolve each otherTypeScript @types 模块无法相互解析
【发布时间】:2017-04-10 19:00:45
【问题描述】:

当我使用 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 定义的最佳方式是什么?

据我所知,typings 已被弃用,所以我尝试从 @types 安装类型定义,然后使用

tsc --target ES5 --module commonjs index.ts

但它还没有工作。我做错了什么?

【问题讨论】:

  • 你有没有想过这个问题?
  • @lonewarrior556 我最终使用了旧的打字工具,但如果我没记错的话,我的一些新项目使用新的@types/* 模块没有问题。在处理客户端服务器代码时要小心,您可能需要使用多个打字稿实例。

标签: node.js typescript typescript2.0


【解决方案1】:

碰巧这是相关的,在一个项目中,我在 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';

问题消失了。

【讨论】:

  • 这是给我的,它让我发疯!谢谢! import {Request} from 'express';做到了
猜你喜欢
  • 2018-11-10
  • 2020-11-01
  • 2017-09-21
  • 2017-05-22
  • 2017-11-11
  • 2017-01-28
  • 2020-04-29
  • 2023-03-09
相关资源
最近更新 更多