0

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)
                 ~~~~~~~

1 Answer 1

1

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.');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.