jus*_*.me 6 node.js typescript typescript2.0
当我使用 npm 像这样安装 TypeScript 定义时
npm install --save @types/express
Run Code Online (Sandbox Code Playgroud)
我无法使用已安装的模块,因为它们无法相互解析。例如,@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'.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?安装 TypeScript 定义的最佳方法是什么?
据我所知,打字已被弃用,所以我尝试从 @types 安装类型定义,然后使用
tsc --target ES5 --module commonjs index.ts
Run Code Online (Sandbox Code Playgroud)
但它还不起作用。我究竟做错了什么?
碰巧这与此相关,在一个项目中,我将泛型类型与 Express 一起使用:
import { ParamsDictionary, Request, ... } from 'express-serve-static-core';
// ...
app.get<ParamsDictionary, UserDetails>(...);
Run Code Online (Sandbox Code Playgroud)
那行得通。当我尝试在另一个不使用泛型类型的项目中做同样的事情时:
import { Request, Response } from 'express-serve-static-core';
Run Code Online (Sandbox Code Playgroud)
我收到“无法解析模块‘express-serve-static-core’的路径”。
当我将导入更改为:
import express, { Request, Response } from 'express';
Run Code Online (Sandbox Code Playgroud)
问题就消失了。
归档时间: |
|
查看次数: |
13579 次 |
最近记录: |