Ghandy
Ghandy

Reputation: 3

importing a typescirpt interface object/variable from a file

i want to import a interface object/variable from a file

the file im trying to import is ping.js:

import { Command } from '../interfaces';

async function execute(argument: Command.arguments) : Promise<void> {
    argument.message.reply('ping = '+ argument.client.ws.ping)
    return;
}

export = {
  name: 'ping',
  perms: 'NONE',
  description: 'Replys with bot ping',
  category: '',
  example: 'ping',
  execute: execute
};

importing code:

glob.glob(`${__dirname}/Commands/**/*{.ts,.js}`, {}, (err, files) => {
   files.forEach((file) => {
      const command : interfeces.Command.interface_ = import(file)
      this.commands.set(command.name , command)
   });
})

interface code:

  export interface interface_
  {
    name: string,
    perms:string,
    description: string,
    category: string,
    example : string,
    execute: (argument: arguments) => Promise<void>
  }

the error i get:

src/Client.ts:33:15 - error TS2740: Type 'Promise<any>' is missing the following properties from type 'interface_': name, perms, description, category, and 2 more.

33         const command : interfeces.Command.interface_ = import(file)
                 ~~~~~~~

Upvotes: 0

Views: 92

Answers (1)

emeraldsanto
emeraldsanto

Reputation: 4741

Using import directly in code (that is, as a function and not a keyword), makes it behave asynchronously. This is where the Promise<any> is coming from - import(file) returns a Promise that will resolve to the desired module, you would need to await it like so:

async function main() {
  try {
    const someModule = await import('your/path/here');
  } catch (error) {
    console.log('Could not import file.');
  }
}

Upvotes: 1

Related Questions