wonea
wonea

Reputation: 4969

Importing class modules into another class

I'm attempting to import some classes into my main class, but get an error; Uncaught SyntaxError: Unexpected token import on the line import ApiDto = Model.DtoModel.ApiDto;

enter image description here

There's two DTO models sharing the same module name across two files;

export module DtoModel { 
    export class ApiDto {
    }
    export class ApiDtoItem { 
        public method: string;
        public object: any;
    }
}

export module DtoModel { 
    export class User { 
        public userNo: number;
    }
}

This is the project structure;

enter image description here

Main.ts

/// <reference path="jquery.d.ts" />

import * as Model from "../DtoModels/./ApiDto";

// breaks on next line!
import ApiDto = Model.DtoModel.ApiDto;

export class Main {
    private Url: string;

    constructor() {
        var apidto = new ApiDto();
        this.Url = "http://localhost:80/api/main";

        var response = jQuery.post(this.Url, (data: ApiDto, textStatus: string, jqXHR: JQueryXHR) => {
           apidto = data;
        });

        console.log(response);
    }
}

var main: Main = new Main();

Here are my compile options:

{
  "compilerOptions": {
    "target": "es6",
    "noImplicitAny": true,
    "noEmitOnError": false,
    "sourceMap": true,
    "inlineSources": true
  },
  "compileOnSave": true
}

Upvotes: 0

Views: 188

Answers (1)

lena
lena

Reputation: 93748

You are compiling your typescript to ES6 ("target": "es6" in tsconfig.json). But the browser doesn't natively support ES6 imports/exports - thus the error.

Upvotes: 1

Related Questions