Shaban Shaame
Shaban Shaame

Reputation: 62

Typescript call a javascript function in current scope

I'm migrating a project from JS to Typescript. But new TS class should coexist with previous JS. There is a function in scope called verifyActivityChange(). In javascript this function has been declared in the app.js file and can be called from anywhere.

How do I call this function from a Typescript class ?

In some TS file I was able to do

    // @ts-ignore
    import * as appjs from "./app.js";
    appjs.verifyActivityChange();

Note sure if this is correct since I had to ignore compilation error. But if I'm doing the same process within a class I have an error appjs dosn't have a constructor.

Edit How do I call the function from the class

    import {verifyActivityChange} from "./app.js";
    
    export class ViewController {
    
        public update(){
    
    verifyActivityChange(); //how can I call this function ? it's on my page DOM

    }

}

Upvotes: 0

Views: 241

Answers (1)

Zer0
Zer0

Reputation: 1690

You can import JS files in TS code, but you have to do inform the compiler that you are trying to do this. You can either compile with the --allowJs option (read more here) or specify this in the tsconfig.json file, read more here)

Upvotes: 1

Related Questions