Gabor Hartyandi Gabor Hartyandi - 2 months ago 10
TypeScript Question

How to import enum in Typescript

I have the following two files: bag.js

import {BagType} from "./bagType" //this line of code gives an error message: bagtype.ts is not a modul
import {Present} from "./present";

export class Bag {

private maxWeight: number;
private bagType: BagType;
private presents: Array<Present> = new Array<Present>();

constructor(maxWeight: number, bagType: BagType) {
this.maxWeight = maxWeight;
this.bagType = bagType;
}

public addPresent(present: Present){
this.presents.push(present);
}
}


and bagType.js

enum BagType {

Canvas,
Paper
}


My question is the next: How can I import the enum BagType to the Bag class?

Answer

I tried it and it worked with:

export enum BagType{
    Canvas,
    Paper
}

and

import {BagType} from "./bagType";