1

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?

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect!! So, considering the steps: 1) Go to node_modules/reverso-api, and create a index.d.ts, with declare module 'reverso-api'; written inside. 2) in a file, for example, bar.ts, I have to import it with the statement import {reverso-api} from reverso-api ?
@LeonardoCarlassare You can create a .d.ts file anywhere in your code, add this line and be done. Afterwards you can import and use this package as normal, nothing changes const Reverso = require('reverso-api'); (just make sure this is the correct usage of the package). I don't recommend put the type declaration inside node_modules though, it should be checked in with git. If this helps, can you upvote and mark is the accepted answer to help others with the same question? ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.