Markus G.
Markus G.

Reputation: 1718

Typescript, class is not defined

I've searched and the answers I found didn't help me.

I created a class in Typescript and wanted to import it to another Typescript-File via

import '../EventDTO';

Than I looked into the converted file (main.js) where all my typescript files are converted to. In there, there's also the class which I've written, but when I want to use it in my file like:

eventList[i] = new EventDTO(data[i].id);

I get this Error in my browser:

Uncaught ReferenceError: EventDTO is not defined

EventDTO class:

class EventDTO{

    id: number;

    constructor(_id: number){
            this.id = _id;
    }

    getId(){
        return this.id;
    }

So, how can I do this correctly?

Upvotes: 11

Views: 13748

Answers (2)

cyr_x
cyr_x

Reputation: 14267

Just export the class in EventDTO.ts:

export class EventDTO { ... }

Upvotes: 0

Radim Köhler
Radim Köhler

Reputation: 123901

You would need to add export keyword

export class EventDTO{

    id: number;

    constructor(_id: number){
            this.id = _id;
    }

    getId(){
        return this.id;
    }

Upvotes: 19

Related Questions