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
JavaScript
x
21
21
1
import queryString from "querystring";
2
import request from "request";
3
import { config } from "./config";
4
5
function translate(text: string, from: string, to: string) {
6
const requestOptions = getRequestOptions();
7
const params = {
8
"from": from,
9
"to": to,
10
"text": text
11
};
12
13
request.get(
14
config.speech.translateApi.endPoind + "/Translate?" + queryString.stringify(params),
15
requestOptions,
16
(error, response, body) => {
17
console.log(body);
18
}
19
);
20
}
21
Advertisement
Answer
If you’re using a typescript project then you can do:
JavaScript
1
8
1
export const translate = () => {}
2
3
4
5
// anotherFile.js
6
7
import {translate} from './translate'
8
However even if you import it this way typescript will still need to compile your code before it can be used.