'No access to optional variables on interface derived class using import function

I'm currently trying some stuff out using a webserver package (express) using typescript i learned from university.

so i want to import all classes in a folder (derived of an interface) and assign my http method endpoints using these classes without hard coding them on initialization. i am able to access my "none optional" variables but my "optionals" always seem to be undefined?

Route initialization:

Here i read the file names of a specified folder and attempt to import the classes based on concataneted root + filename and assign the callback to a method if its defined

// Route initialization function
const loadServerRoutes = async() => 
{
    for(const filename of fs.readdirSync(`./${app.get("page root")}`))
    {
        const filepath = `./${app.get("page root")}/${filename}`;

        let page: any = (await import(filepath)).default;
        let pageInstance: IPage = new page();
        let endpoint: string =  pageInstance.endpoint === undefined ? filename.substring(0, filename.length-3) : pageInstance.endpoint;

        for(let httpmethod of ["get", "put", "post", "delete", "connect", "head"])
        {
            if(!Object.keys(pageInstance).includes(httpmethod))
                continue;

            console.log(httpmethod);
        }
        console.dir(pageInstance);
    }
}

Interface classes are derived of:

import IWebRequest from "./IWebRequest";
import UserRights from "./UserRights";

/**
 * Interface for page classes.
 */
interface IPage 
{
    menu_text: string, 
    user_level: UserRights, 
    endpoint?: string, 
    get?: IWebRequest,
    post?: IWebRequest,
    head?: IWebRequest,
    put?: IWebRequest, 
    delete?: IWebRequest,
    connect?: IWebRequest
}

export default IPage;

class attempting to load:

get method has been defined as public, same as the required properties but this doesnt seem to get detected?

import IWebRequest from '../server/IWebRequest';
import IPage from '../server/IPage';
import UserRights from '../server/UserRights';

/**
 * The main entry point of the website, on or without session
 */
class Index implements IPage
{
    public user_level: UserRights = UserRights.Guest;
    public menu_text: string = "home";

    public get (request: any, response: any): IWebRequest
    {
        return response.render("index");
    }
}


export = Index;

Current output:

Index { user_level: 0, menu_text: 'home' }


Solution 1:[1]

Apparently for some bizare reason, the functions are not available trough Object.keys, neither trough console.dir, the functions are accessible but not visible for display trough this method.

So a possible solution would be just checking the methods without using the object.keys function and mapping the interface to a key, value based object and checking or the specified key is undefined

const loadServerRoutes = async() => 
{
    for(const filename of fs.readdirSync(`./${app.get("page root")}`))
    {
        const filepath = `./${app.get("page root")}/${filename}`;

        let page: any = (await import(filepath)).default;
        let pageInstance: {[key: string]: any} = new page();
        let endpoint: string =  pageInstance.endpoint === undefined ? filename.substring(0, filename.length-3) : pageInstance.endpoint;

        for(let httpmethod of ["get", "put", "post", "delete", "connect", "head"])
        {
            if(pageInstance[httpmethod] === undefined)
                continue;


            console.log(httpmethod);
        }

        console.dir(pageInstance.get);
        console.dir(pageInstance);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jannick Oste