0

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

    }

}

1 Answer 1

1

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)

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

2 Comments

then I think the question is more about the scope. How can I call the function from a typescript class ? Knowing the app.js has been included in the index.html
Note I had to convert app.js to a module otherwise I couldn't import typescript classes

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.