I want to use the below given function called “translate” in a JavaScript file. I have seen a answer on stackoverflow regarding this, but couldn’t get what I had to do. Definitely the normal calling of function isn’t working in this case
import queryString from "querystring"; import request from "request"; import { config } from "./config"; function translate(text: string, from: string, to: string) { const requestOptions = getRequestOptions(); const params = { "from": from, "to": to, "text": text }; request.get( config.speech.translateApi.endPoind + "/Translate?" + queryString.stringify(params), requestOptions, (error, response, body) => { console.log(body); } ); }
Advertisement
Answer
If you’re using a typescript project then you can do:
export const translate = () => {} ... // anotherFile.js import {translate} from './translate'
However even if you import it this way typescript will still need to compile your code before it can be used.