admin管理员组文章数量:1389838
Now I am sure the issue is because there is a d.ts file included which contains a module called "Shared", and a require statement which includes a variable of the same name if it is being used in a NodeJS environment.
// shared.d.ts
declare module Shared { ... }
// other_module.ts
/// <reference path="shared.d.ts"/>
if(require) { var Shared = require("shared"); }
export class Something {
public someVar = new Shared.SomethingElse("blah");
}
So when I pile other_module.ts
(which is actually a lot of separate files), it tells me Shared is a duplicate identifier, which I can understand as TS thinks Shared is a module, but then is being told it is the return of require.
The problem here is that the output of modules need to be patible with nodeJS's require system, so in this case when other_module is required it will be in its own scope and will not know about Shared.SomethingElse
so the require is needed so the internal modules in other_module
will be able to access the Shared library, but in the browser environment it would get Shared.SomethingElse
via the global scope.
If I remove the reference then the file wont pile as it doesn't know about Shared
, if I remove the require when the module is loaded into nodejs (var otherModule = require("other_module")
) it will plain that it doesn't know about Shared
. So is there a way to solve this?
Now I am sure the issue is because there is a d.ts file included which contains a module called "Shared", and a require statement which includes a variable of the same name if it is being used in a NodeJS environment.
// shared.d.ts
declare module Shared { ... }
// other_module.ts
/// <reference path="shared.d.ts"/>
if(require) { var Shared = require("shared"); }
export class Something {
public someVar = new Shared.SomethingElse("blah");
}
So when I pile other_module.ts
(which is actually a lot of separate files), it tells me Shared is a duplicate identifier, which I can understand as TS thinks Shared is a module, but then is being told it is the return of require.
The problem here is that the output of modules need to be patible with nodeJS's require system, so in this case when other_module is required it will be in its own scope and will not know about Shared.SomethingElse
so the require is needed so the internal modules in other_module
will be able to access the Shared library, but in the browser environment it would get Shared.SomethingElse
via the global scope.
If I remove the reference then the file wont pile as it doesn't know about Shared
, if I remove the require when the module is loaded into nodejs (var otherModule = require("other_module")
) it will plain that it doesn't know about Shared
. So is there a way to solve this?
1 Answer
Reset to default 6First the error
Duplicate identifier because you have Shared
in shared.d.ts
+ in other_module.ts
.
FIX A, be all external
If you want to use amd
/ monjs
ie. external modules, you need to use import/require
(not var/require
like you are doing). Using an import
creates a new variable declaration space and therefore you are no longer polluting the global namespace Shared
from other_module.ts
. In short :
// shared.d.ts
declare module Shared {
export function SomethingElse(arg:string):any;
}
declare module 'shared'{
export = Shared;
}
And a typesafe import:
// other_module.ts
/// <reference path="shared.d.ts"/>
import Shared = require("shared");
export class Something {
public someVar = new Shared.SomethingElse("blah");
}
FIX B, as you were, but you need to use a different name then
Inside other_module
don't use the name Shared
locally if local scope is global scope. I remend you just use external everywhere and pile for node with monjs
and browser with amd
as shown in fix A, but if you must here is a pile fixed other_module.ts
.
// other_module.ts
/// <reference path="shared.d.ts"/>
var fooShared: typeof Shared;
if(require) { fooShared = require("shared"); }
else { fooShared = Shared; }
export class Something {
public someVar = new fooShared.SomethingElse("blah");
}
本文标签: javascriptDuplicate identifier error with dts file and nodejs require with same nameStack Overflow
版权声明:本文标题:javascript - Duplicate identifier error with d.ts file and nodejs require with same name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744701949a2620611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论