admin管理员组文章数量:1402783
I want to know to import Javascript module in Typescript.
Project
- Module is "amd."
- Use outFile option for single file.
- Control internal module from
///<reference path=''/>
code
app.js
export function func(a, b) {
return a+b;
}
MainApp.ts
import Stats from "../app";
class MainApp {
foo() {
const f = func(1, 2); // not define (runtime error)
}
}
error
SyntaxError: Unexpected token export
ReferenceError: define is not defined
Main.js:6667
at d:\...\Main.js:6667:2
ReferenceError: MainApp is not defined
at window.onload (d:\...\index.html:18:24)
not found define.
I want to know to import Javascript module in Typescript.
Project
- Module is "amd."
- Use outFile option for single file.
- Control internal module from
///<reference path=''/>
code
app.js
export function func(a, b) {
return a+b;
}
MainApp.ts
import Stats from "../app";
class MainApp {
foo() {
const f = func(1, 2); // not define (runtime error)
}
}
error
SyntaxError: Unexpected token export
ReferenceError: define is not defined
Main.js:6667
at d:\...\Main.js:6667:2
ReferenceError: MainApp is not defined
at window.onload (d:\...\index.html:18:24)
not found define.
Share Improve this question edited Nov 28, 2018 at 12:22 Dubs asked Nov 28, 2018 at 10:07 DubsDubs 791 silver badge9 bronze badges 1- Possible duplicate of How to import js-modules into TypeScript file? – d4rty Commented Nov 28, 2018 at 15:05
1 Answer
Reset to default 5It is possible there is mistake with default exports. Here is working example appropriate to your code structure.
tsconfig.json:
{
"pilerOptions": {
"strict": true,
"allowJs": true,
"target": "es6",
"module": "monjs"
}
}
app.js:
export function sum(a, b) {
return a + b;
}
MainApp.ts:
import {sum} from './app';
class MainApp {
foo() {
const a = 1;
const b = 2;
const result = sum(1, 2);
}
}
global.d.ts:
declare module '*.js';
本文标签: How to import Javascript module in TypescriptStack Overflow
版权声明:本文标题:How to import Javascript module in Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744374688a2603200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论