Krish11
Krish11

Reputation: 33

How to use a TypeScript function in Javascript

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);
    }
);
} 

Upvotes: 0

Views: 47

Answers (1)

user16435030
user16435030

Reputation:

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.

Upvotes: 1

Related Questions