声明合并和别名不能一起工作

Declaration merging and aliasing not working together

我最近开始将我的节点。js/express 应用程序转换为打字稿。

到目前为止它运行良好,但有些事情我不确定:

我注意到在 sample project by Microsoft 中,它们与他们的打字不一致。

app.ts

app.get('/findImages', function(req, res) {
    // ...
}

routes/index.ts

export function index(req: express.Request, res: express.Response) {
    // ....
};

如您所见,它们有时会定义类型,有时不会。我注意到第一种情况下没有 IntelliSense,但这也可能是 IntelliJ 的问题。这里的最佳做法是什么?

起初我以为我会输入所有内容,但后来我注意到另一个奇怪的行为:

app.ts

/// <reference path="connect-flash/connect-flash.d.ts" />
/// <reference path="express-session/express-session.d.ts" />
/// <reference path="express/express.d.ts" />
/// <reference path="passport/passport.d.ts" />

import express = require('express');
import session = require('express-session');
import passport = require('passport');
import flash = require('connect-flash');

var app: express.Express = express();

// Express without capital letter
route.get('/', function (req: express.Request, res: express.Response): void {
    req.flash('message');       // no IntelliSense
    var session = req.session;  // no IntelliSense
    var ip = req.ip;            // works
    var test = req.params.test; // works
});

// Express with capital letter
route.get('/', function (req: Express.Request, res: Express.Response): void {
    req.flash('message');       // works
    var session = req.session;  // works
    var ip = req.ip;            // no IntelliSense; compile error TS2339
    var test = req.params.test; // no IntelliSense; compile error TS2339
});

我收到以下错误:

Error:(54, 22) TS2339: Property 'ip' does not exist on type 'Request'.
Error:(55, 24) TS2339: Property 'params' does not exist on type 'Request'.

我查看了定义文件并注意到显然有两种不同的方式来定义模块:

express.d.ts

declare module Express {...}
declare module "express" {...}

我尝试了多种大小写组合(也在其他定义文件中)但没有成功。

好像express模块​​有两个单独的定义。第一个也存在于其他模块中,例如 express-session.d.tsconnect-flash.d.ts,并且它们正确地合并在一起。但是express.d.ts文件中的大小写差异好像有问题。有没有办法合并它们?

感谢您的帮助。

As you can see they are sometimes defining types, sometimes not. I noticed that there is no IntelliSense in the first case, but this might also be an issue of IntelliJ. What would be the best practice here

我尽量在任何地方都直截了当。但是如果打字稿可以为你推断它,你就可以避免这种情况。

I am getting the following errors:

如果 ip 是明确请求的一部分,那么这只是 express.d.ts 中的遗漏,需要修复。

noticed that there are apparently two different ways to define a module:

这两个 不同 类型的模块。 declare module foo 表示这是一个全局变量 foo 可用。 declare module "foo" 意味着你可以做到 import foo = require('foo').

But there seems to be a problem with the upper and lower case difference in the express.d.ts file. Is there a way to merge them

他们应该都有相同的案例。