0

I'm developing mobile applications using Visual Studio 2017 and Apache Cordova. I've always coded using JQuery and Javascript however i have never written a code using Typescript.

When searching the web almost everyone is using Typescript with Ionic. (thinking of starting to benefit and use the Ionic Framework) Is it the same if i use JQuery or Javascript instead of Typescript with Ionic?

Or its recommended that i learn Typescript and start using it with Ionic?

Thank you for the clarification.

This is how i usually write the code for Cordova using JQuery:

function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}
return array;
}

function beginGame() {

$('#info').hide();

var level = 0;

$('#game_title h2').html('Level 1');
$('#game_question h3').html('What is the most common language?');

$('#game_body').html('');
var gameBody = document.getElementById('game_body');
var columnAnswer, buttonAnswer;

var answers = [['Arabic', ''], ['Spanish', ''], ['English', 'right_answer'], ['Chinese', '']];
var arr = shuffleArray(answers);
console.log(arr);

for (var i = 0; i < arr.length; i++) {
    columnAnswer = document.createElement('div');
    columnAnswer.className = 'col-xs-12';

    buttonAnswer = document.createElement('button');
    buttonAnswer.id = 'answer_button_'+i;
    buttonAnswer.className = 'btn btn-primary btn-sx answer_button ' + arr[i][1];
    buttonAnswer.type = 'button';
    buttonAnswer.innerHTML = arr[i][0];
    //buttonAnswer.setAttribute('onclick', 'onQuoteClick(' + quotes[i].id + ')');
    console.log(buttonAnswer);
    columnAnswer.appendChild(buttonAnswer);

    gameBody.appendChild(columnAnswer);
}

$('.answer_button').click(function () {
    var button_id = $(this).attr('id');
    if ($('#' + button_id).hasClass('right_answer')) {
        //alert('Right Answer');
        $('#info').show();
        fillInformation();
    } else {
        alert('Wrong Answer');
    }
});



}
function fillInformation() {
$('#game_title h2').html('English');
$('#info_image img').attr("src", './images/level_1.png');
$('#info_text h5').html('English Language');
}
1
  • TypeScript is a superset of JavaScript. Unless you enable more strict rules like noImplicitAny or strictNullChecks, you can just use regular JavaScript in .ts files. Using TypeScript also has no effect on what libraries (like jQuery) you can use. Commented Jun 5, 2017 at 17:33

2 Answers 2

2

Yes, it's totally worth working with Typescript and doing it the standard way.

The main difference with what you are used to is not Typescript vs Javascript, it's vanilla Js vs Angular.

Angular is the framework upon which Ionic rests and which it is extending. The Angular framework is by default in Typescript.

But that shouldn't be a problem for you, since Typescript is backwards compatible with Javascript. Any vanilla Js code in a .ts file compiles and runs just fine (although you are losing on the benefits of Typescript if you are not following best practices).

You can also definitely use jquery, just like you are used to.

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

2 Comments

Thank you that made it clear for me. I took a look at typescript its closer to Java code then javascript right?
I'm glad.Exactly. I strongly suggest you train yourself to always using types (no any type) and work your way up to specifying the flag 'noImplicitAny': true you your tsconfig.json, which is the Typescript compiler's configuration. I also have an opinionated tslint.json file you could download and copy into your project. If you want to learn a bit more on why it's worth getting into Typescript, I recently wrote a Medium article on the matter.
1

You should be able to use JQuery and Javascript with Ionic, however, you may lose the Angular capabilities Ionic offers. You could write ES6 javascript in the ts files as well. Otherwise, I'd recommend using Typescript which isn't too difficult to learn imo. Hope this helps.

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.