简体   繁体   中英

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?

I tried it and it worked with:

export enum BagType{
    Canvas,
    Paper
}

and

import {BagType} from "./bagType";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM