Home > Mobile >  Importing a JS module in a Typescript file
Importing a JS module in a Typescript file

Time:06-13

I am developing a website in TS in which I have to call an unofficial API, that is https://www.npmjs.com/package/reverso-api. The whole module is written in JS, and as described in the docs, the proper way to import the module in JS is

const Reverso = require('reverso-api');
const reverso = new Reverso();

Unluckily, I am developing in TypeScript. I have no idea how to import the module in my project in order to make it works. How can I do that?

CodePudding user response:

If this package doesn't have a type definition, you can use a temporary shorthand declaration so TypeScript won't yell at you. This makes all imports from reverso-api have any type, which you might have guessed is not very safe, but it's all we have right now.

declare module 'reverso-api';

Reference: https://www.typescriptlang.org/docs/handbook/modules.html#shorthand-ambient-modules

  • Related