How to import Javascript module in Typescript - Stack Overflow

I want to know to import Javascript module in Typescript.ProjectModule is "amd."Use outFile

I want to know to import Javascript module in Typescript.

Project

  1. Module is "amd."
  2. Use outFile option for single file.
  3. 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

  1. Module is "amd."
  2. Use outFile option for single file.
  3. 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
Add a ment  | 

1 Answer 1

Reset to default 5

It 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';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744374689a4571126.html

相关推荐

  • How to import Javascript module in Typescript - Stack Overflow

    I want to know to import Javascript module in Typescript.ProjectModule is "amd."Use outFile

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信