1

Hi i need to call a normal javascript function from a typescripit file.

Assume that i have a javascript file names test.js and i have imported this file in the index.html of the application.

Now i need to call this test() function from app.component.ts file.

function test(){

cosole.log('test');

}
3

2 Answers 2

6

This works for me

(window as any).myJavascriptFunction()
Sign up to request clarification or add additional context in comments.

Comments

2

I think there is no default way to in JavaScript. What you can do is write your JS file like this.

className/test.js

class className {
   function test() {
     console.log('test');
}
exports.default = className;

Create a Type definition.

className/index.d.ts

export default class className {
   test(): void;
}

You can now use your JS in app.component.ts.

import className from "./className";

var t = new className();
t.test();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.