admin管理员组文章数量:1241062
I had this file:
//foo.js
var foo = function () {
return "foo";
};
module.exports = foo;
So, I wanted to import it to my Typescript file. I tried this
//typescript.ts
import * as foo from ("./foo");
Didn't work. I read about this 'ambient' modules, so I added this
//typescript.ts
/// <reference path="./foo.d.ts" />
import * as foo from ("./foo");
And I added a "foo.d.ts" file in the same folder, which had the purpose of letting typescript know about the types of my imported function:
declare module "foo"
{
function foo(): string
export = foo;
}
No luck.
I thought that the problem was with the import syntax (you cannot have the same syntax for es6 and monjs modules, right?). So I did this.
import foo = require("./foo");
As you might guess, that didn't work either.
I have been able to import d3 an use it successfully when I installed it as a node module with npm install d3
and referenced its d.ts
file. I did it with this code:
import * as d3 from "d3";
I haven't been able to do the same with any other module (jquery, box2d, box2d-monjs, among others), nor with my own libraries as demonstrated above. I am new to Typescript, so probably I'm missing something very obvious, but I haven't been able to figure it out by myself.
I had this file:
//foo.js
var foo = function () {
return "foo";
};
module.exports = foo;
So, I wanted to import it to my Typescript file. I tried this
//typescript.ts
import * as foo from ("./foo");
Didn't work. I read about this 'ambient' modules, so I added this
//typescript.ts
/// <reference path="./foo.d.ts" />
import * as foo from ("./foo");
And I added a "foo.d.ts" file in the same folder, which had the purpose of letting typescript know about the types of my imported function:
declare module "foo"
{
function foo(): string
export = foo;
}
No luck.
I thought that the problem was with the import syntax (you cannot have the same syntax for es6 and monjs modules, right?). So I did this.
import foo = require("./foo");
As you might guess, that didn't work either.
I have been able to import d3 an use it successfully when I installed it as a node module with npm install d3
and referenced its d.ts
file. I did it with this code:
import * as d3 from "d3";
I haven't been able to do the same with any other module (jquery, box2d, box2d-monjs, among others), nor with my own libraries as demonstrated above. I am new to Typescript, so probably I'm missing something very obvious, but I haven't been able to figure it out by myself.
Share Improve this question asked Apr 9, 2016 at 0:10 FedericoFederico 3,9205 gold badges36 silver badges50 bronze badges 2-
Did you try requiring it as
"./foo.js"
instead of just"./foo"
? – TAGraves Commented Apr 9, 2016 at 1:39 - Yes. With all of the above. – Federico Commented Apr 9, 2016 at 1:57
1 Answer
Reset to default 9Turns out it was something really obvious: you had to use the --allowJs
option. This worked for me:
tsc --moduleResolution "node" --module "monjs" --allowJs main.ts
Though, I still can't figure out why d3 worked while the others libraries didn't.
本文标签: javascriptCan Typescript import CommonJS ModulesStack Overflow
版权声明:本文标题:javascript - Can Typescript import CommonJS Modules? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740056000a2222369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论