1

I am creating an application by TypeScript and using WebPack to transpile and bundle. I am using the final result of this application in a pure JavaScript website. In this application I have defined an enum as below:

export const enum ShapeType {
    Actor,Ellipse,Border,Connector
}

I also exported it as:

export { ShapeType } from "./shape/shape.type";

But when I try to use it like:

 var createdShape = new shapeFactory.createShape(ShapeType.Ellipse);

It does not create a shape and when I debug I see this error:"ShapeType is not defined" I also try to find ShapeType in the final JavaScript bundle file, but I found out there is no ShapeType in the bundle file too. I don't have a problem when I import it inside TS.

The code below is the js code. ChartDraw is the library name I defined in my webpack config file. All other functions work fine. The only problem is with

var aa = shapeFactory.createShape(ChartDraw.ShapeType.Ellipse); 

because ShapeType is not defined.

   var svg = new ChartDraw.Svg("drawing");
    var shapeFactory = new ChartDraw.ShapeFactory(svg);
    var ob1 = null;
    var ob2 = null;
    //aa.draw();
    var cc = new ChartDraw.Connector(svg);
    var bb = new ChartDraw.MouseReader();
    bb.setExportFunction(info => {
        var aa = shapeFactory.createShape(ChartDraw.ShapeType.Ellipse);
        aa.rectangularPosition = info;
        aa.draw();
        if (ob1 == null)
            ob1 = aa;
        else {
            ob2 = info;
            cc.beginObject = ob1;
            cc.endObject = aa;
            cc.draw();
        }
    });

And the code below is where I import ShapeType:

import { ShapeType } from "./shape.type";
import { Actor } from "./actor";
import { Svg } from "../svg";
import { Shape } from "./shape";
import { Ellipse } from "./ellipse";

export class ShapeFactory {
    private svg: Svg;
    constructor(svg: Svg) {
        this.svg = svg;
    }
    public createShape(shape: ShapeType):Shape {
        switch (shape) {
            case ShapeType.Actor:
                let actor = new Actor(this.svg);
                return actor;
            case ShapeType.Ellipse:
                let ell = new Ellipse(this.svg);
                return ell;
        }
    }
}
8
  • Possible duplicate of How to import an Enum Commented Feb 23, 2019 at 11:08
  • Not really, it is a different story. I have this issue when I convert it to JS. When I import it in another TS file it works fine. Commented Feb 23, 2019 at 11:11
  • I guess you didn't import ShapeType. You need perhaps import {ShapeType} from 'pathOfJSFile'; Commented Feb 23, 2019 at 11:11
  • Can you share your JS file ? If the issue is with JS and not TS. That will help in quick debug. Commented Feb 23, 2019 at 11:13
  • Surely this line export { ShapeType } from "./shape/shape.type"; should be import { ShapeType } from "./shape/shape.type"; Commented Feb 23, 2019 at 11:19

1 Answer 1

1

Eventually, I found out what is the problem. The first problem was because of the const. When typescript code is converting to JavaScript, an enum converts to an object, but if I add the const keyword it is not an object anymore. So to fix it I removed the const as below:

export enum ShapeType {
    Actor,Ellipse,Border,Connector
}

The second issue was in my JavaScript code. At first step, I forgot to add library name(ChartDraw) to the ShapeType when calling shapeFactory.createShape(ChartDraw.ShapeType.Ellipse)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.