1

I am trying to call typescript method from Javascript method in IONIC. But typescript method is not getting called. I have created a Cordova plugin in IONIC that will return the result in Javascript success or failure method. From the success method, I would like to call typescript method to navigate to another page in ionic. Please let me know how to call typescript from Javascript.

Code snippet:

var success= function(){
    alert("JS method called");.       // alert displayed 
    typescriptMethod();.         // Defined in class of typescript
}

Class MainClass
{
     constructor()
     {
         success();.   // JS method called 
     }
     typescriptmethod()
     {
         alert("typescript method called");.   // alert not displayed
     }
}

Thanks,

2
  • Are you certain the problem has to do with TypeScript? There are a lot of reasons a method might not get called. Once TypeScript is compiled it's just JavaScript, so assuming it's being compiled successfully then you need to diagnose this problem by looking at the compiled JS. Have you stepped through the code in your debugger to see what's actually happening? Your example doesn't help us reproduce the problem or really give us any idea of what's going on. Commented Jan 11, 2019 at 17:38
  • You would need to create an instance of MainClass first. new MainClass().typescriptmethod(); Commented Jan 11, 2019 at 17:45

2 Answers 2

2

You must understand that TypeScript is not a stand-alone programming language. It comes on-top of JavaScript, as a superset ( which means that once a TypeScript program is written and compiled, it is essentially a JavaScript program ). You could easily try writing the equivalent JavaScript code, if you want to do the trick. If you go to the TypeScript Playground you could really fast transpile your TS code into JS code ( if not too complex of course ) . Anyway , here is a bit of a refresher how can you achieve what you asked :

     var success = function () {
    alert("JS method called");
    typescriptMethod();
};
var MainClass = /** @class */ (function () {
    function MainClass() {
        success();
    }
    MainClass.prototype.typescriptmethod = function () {
        alert("typescript method called");
    };
    return MainClass;
}());

This will give an error that typescripmethod() is not deifned , so you could make it static ( JS code) :

var MainClass = /** @class */ (function () {
    function MainClass() {
        success(); // JS method called 
    }
    MainClass.typescriptMethod = function () {
        alert("typescript method called");
    };
    return MainClass;
}());
var success = function () {
    alert("JS method called"); // alert displayed 
    MainClass.typescriptMethod(); // Defined in class of typescript
};
success();

Or use a class instance ( JS code ) :

var MainClass = /** @class */ (function () {
    function MainClass() {
        success(this); // JS method called 
    }
    MainClass.prototype.typescriptMethod = function () {
        alert("typescript method called");
    };
    return MainClass;
}());
var success = function (instance) {
    alert("JS method called"); // alert displayed 
    instance.typescriptMethod(); // Defined in class of typescript
};
success(new MainClass());

Hope this will do the trick for you .

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

Comments

1

you cannor call typescript code from javascript code.

typescript code is compiled into javascript. You can call equivalent javascript code of your typescript code.

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.