DEV Community

Andre Olive
Andre Olive

Posted on

I Need help with typescript classes

Someone may help me?

I'm learning typescript and I have some trouble with classes.
here's my code:

Command Class

import Diagram from "../diagram/Diagram";
import MainEngine from "../MainEngine";
import { CommandData } from "../managers/CommandManager";
import User from "../core-models/UserModel";

export default abstract class Command<D>{
    function: Function;
    data: D;
    user: User;
    engine: MainEngine
    diagram: Diagram
    constructor(commandData:CommandData){
        this.function = async () => {};
        this.data = commandData.data;
        this.user = commandData.user;
        this.engine = commandData.session._engine;
        this.diagram = commandData.session._diagram;
    }

    public async execute(){
        await this.function();
    }

}
Enter fullscreen mode Exit fullscreen mode

and This CommandGetVncUrl that extends Command

import Command from "./Command";
import { CommandData } from "../managers/CommandManager";
import ResponseCommand from "./ResponseCommand";

export default class CommandGetVncUrl extends Command<any>{

    constructor(commandData:CommandData, response:ResponseCommand){
        super(commandData);
        this.function = async () => {
            try {
                const device = this.diagram.selectDeviceById(this.data);
                if(device){
                    const url = await this.engine._cloudMngr.getConsoleUrl(device.getDeviceData(), this.user);
                    response.setData({url}).send();
                }
            } catch (error) {
                throw Error();
            }
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

when I call the function .execute() I have this error:

Property 'execute' does not exist on type 'CommandGetInstanceStatus | CommandDiagramLoad | CommandCreateInstance | CommandChangeDevicePosition | ... 10 more ... | CommandReboot'.
Property 'execute' does not exist on type 'CommandCreateLink'.

74 await this.make(commandData)[commandData.command].execute();

Top comments (2)

Collapse
 
dbugslayer profile image
Daniel Tavares

The object instance stored in this.make(commandData)[commandData.command] doesn't have the execute() it's trying to call. Slap a breakpoint in that line and inspect the value returned by this.make(commandData). You may have to split it up in a few lines to make it easier to debug. I.e.:

var commands = this.make(commandData);
await commands[commandData.command].execute(); // break here
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

You definitely have way more code that is involved with your problem that you show here. Try to create simpler example that reproduce your problem or show your whole code on website like GitHub.