Home > database >  Why is export {} causing Unexpected token. A constructor, method, accessor, or property was expected
Why is export {} causing Unexpected token. A constructor, method, accessor, or property was expected

Time:08-12

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.

CodePudding user response:

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 {};
  • Related