4

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.

Dubs
  • 79
  • 1
  • 8
  • Possible duplicate of [How to import js-modules into TypeScript file?](https://stackoverflow.com/questions/41219542/how-to-import-js-modules-into-typescript-file) – d4rty Nov 28 '18 at 15:05

1 Answers1

5

It is possible there is mistake with default exports. Here is working example appropriate to your code structure.

tsconfig.json:

{
    "compilerOptions": {
        "strict": true,
        "allowJs": true,

        "target": "es6",
        "module": "commonjs"
    }
}

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';
General Grievance
  • 4,555
  • 31
  • 31
  • 45
B. Bohdan
  • 480
  • 4
  • 12