Node.js with typescript require and .d.ts files

别等时光非礼了梦想. 提交于 2020-01-05 22:23:29

问题


I want to create an server side script, for this I require "Q", so I include the q.d.ts file. And I use this to import q:

var Q = require("q");

Now I recive this error: Duplicate identifier 'Q'

Does somebody know how to solve this name conflict?

The complete startup script:+

    ///<reference path="Scripts/typings/q/Q.d.ts" />
///<reference path="Server.ts" />
///<reference path="Services/DatabaseProvider.ts" />
///<reference path="Response/Response.ts" />
///<reference path="Response/DataResponse.ts" />


var Q = require("q");
var di = require("ng-di");
var app = di.module("App", []);
app.service("server", Server);
app.service("$databaseProvider", DatabaseProvider);
app.run((server: Server) => {
    server.run();
});

di.injector(["App"]);

回答1:


Duplicate identifier 'Q'

In the absence of an import or an export statement at the root of your file: Your file as well as any other such file passed to the TypeScript compiler is considered a part of the global namespace. So the variable Q is conflicting with the one declared in q.d.ts.

Fix

import Q, don't just require it :

import Q = require("q");

More about External modules : https://www.youtube.com/watch?v=KDrWLMUY0R0



来源:https://stackoverflow.com/questions/29050092/node-js-with-typescript-require-and-d-ts-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!