Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is export {} causing Unexpected token. A constructor, method, accessor, or property was expected. error?

When trying to make a class in typescript I encountered the issue where ‘Project’ can’t be compiled as a module so I added the empty export export {}.

What I don’t understand is why it now gives the error that it’s an unexpected token. Am I missing something that I should’ve added?

class Project {

    title: string;
    description: string;
    image: HTMLImageElement;

    constructor(
        title: string,
        description: string,
        image: HTMLImageElement
    ) {
        this.title = title;
        this.description = description;
        this.image = image;
    }
    export {};
}

Also not sure what additional info would be useful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot export something from inside a class block.

You can export functions, var, let, const, and — as we’ll see later —
classes. They need to be top-level items; you can’t use export inside
a function, for example. – JavaScript modules (developer.mozilla.org)

What you need to do is move the export statement outside of the class and it will work just fine.

class Project {

    title: string;
    description: string;
    image: HTMLImageElement;

    constructor(
        title: string,
        description: string,
        image: HTMLImageElement
    ) {
        this.title = title;
        this.description = description;
        this.image = image;
    }
}

export {};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading