-2

I have a function in my TypeScript file:

public test(): string[] {
    return new Array("Mary", "Tom", "Jack", "Jill");
}

How do I call this function from JavaScript?

5
  • You create the class and invoke the method .. just as you normally would .. complications might arise if you use a module system but that is a general JS issue not so much TS Commented Jul 1, 2019 at 8:29
  • 1
    test()....... Commented Jul 1, 2019 at 8:30
  • 3
    You need to compile the Typescript to Javascript first anyway. Once you do, it's just like any other Javascript. Commented Jul 1, 2019 at 8:30
  • This could be interesting for you Commented Jul 1, 2019 at 8:40
  • Does this answer your question? Calling properly TypeScript code from JavaScript Commented Jul 23, 2021 at 14:15

2 Answers 2

3

TypeScript files are source files that are not run directly, they are transpiled to JavaScript before they can be run. Normally project using TypeScript have some sort of build pipeline setup using something like Webpack, you cannot just use a TypeScript file directly with a JavaScrpt project.

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

3 Comments

So what can we do now? I need to get the list of data from the vscode extension (typescript) to vscode webview script part (javascript).
That is not in your question. So to get a better answer, include all code so that others can reproduce your situation.
1

TypeScript is compiled to JavaScript before anything else happens - so once it's compiled, you call it later in your project just like you would any other function:

test();

1 Comment

actually i guess i have to import the .js file which is a compiled .ts file but writing that in script part of html is really not working

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.