0

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);
    }
);
} 
5
  • 1
    You'll have to compile the .ts file to a .js file, then include the .js file and call translate from it. Commented Aug 12, 2021 at 16:14
  • Won't the function give an error if it does not have proper values. As I want to give values to the function when I will it in JS file. So it won't work, i guess Commented Aug 12, 2021 at 16:16
  • Javascript has no concept of types, and Typescript type information does not exist at runtime. If you want type safety you will have to convert your project to use Typescript and rely on the compiler to enforce type safety for you. Commented Aug 12, 2021 at 16:20
  • I guess I have to find some other way to use the Microsoft translator API😅 Commented Aug 12, 2021 at 16:22
  • BTW thanks for your answers and time🙏 Commented Aug 12, 2021 at 16:22

1 Answer 1

1

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.

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

1 Comment

Thanks for answering mate!

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.