2

I have built an algorithm in JavaScript, now I wish to use this algorithm on my ionic 2 application. Ideally, I would like to avoid having to translate the entire algorithm to typescript. So far, i've had some success running javascript in the index.html page but can't seem to be able to CALL those functions from the .ts files.

Can someone please give me some suggestions on ways to integrate my js algorithm in my ionic 2 application, or should i just bite the bullet and use typescript?

Thanks

5
  • 3
    Show us the code. Commented Mar 10, 2017 at 16:46
  • There is no reason to show the code. Commented Mar 10, 2017 at 16:48
  • Yes there's a need. We don't know how your code is, if it's pure JS or if it has jquery, how are you calling it on your HTML file, if you're using it in the right place. So show us the code so we can help Commented Mar 10, 2017 at 17:51
  • 1
    Sure, the code is here: "function hello(){alert('hello world');}" How do i call this function on the .ts files? Commented Mar 10, 2017 at 18:48
  • Posting code is redundant given my circumstances, trust me. I just need advice. Commented Mar 10, 2017 at 18:49

1 Answer 1

1

You would need (.d.ts) typings definition files for this. For example:

If you have demo.js file with following content.

var setUserInfo = function (firstName, secondName) {
    console.log("demo function called: " + firstName + " " + secondName);
}

module.exports = { setUserInfo: setUserInfo };

You would need to make a declaration demo.d.ts file with following:

declare module User {
    function setUserInfo(firstName: string, secondName: string): void;
}

export = User;

Put above two files in one directory. Now if you want to use js in your ts file then follow below steps:

1). import * as _ from './demo'; // first import file. Here ./demo path is relative to your current directory

2). _.setUserInfo("sandeep", "sharma"); // call method

Hope this will help you !!

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

Comments

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.