TypeScript for plugins development

I have been working on some plugins with vanilla JavaScript (ES5) and there was no problem so far; however, for something bigger (Luna Engine for example), I foung it there are some problems with JavaScript itself, so I decided to find a language/tool to help me there, and that's when I tried TypeScript.




Introduction



TypeScript is a superset of JavaScript which will be compiled into plain JavaScript. You can easily code anything in TypeScript and publish as plain JavaScript without any trouble, and they will be compatible with any browser support ES5/6.


TypeScript also takes the class notation in ES6, so coding in TypeScript is not so different to coding in plain JavaScript, except the type/interface defination we will talk below.


Types and Interfaces



TypeScript will check your code before compiling into JavaScript, so that any problem you might meet in development will be catched beforehand. And what awesome is the type and structure checking in TypeScript.


For example, you set variable var str: string, the variable will be forced to be string and will raise an error if you assign different type to that variable.


Furthermore, TypeScript allows us to define a new type of variable in a structure way. For example, we can force an object must have a defined structure:


interface Parameter {
strength: number;
defense: number;
agility: number;
magic: number;
}

interface Actor {
id: number;
name: string;
parameters: Parameter;
}

var actor: Actor;



Those above code will force the Actor type to the variable actor, and the compiler will raise an error if the object actor doesn't have the same structure we defined in the interface Actor.


So what's good?



Firstly, TypeScript syntax and notation are cleaner than ES5, while ES6 is not fully support in most browsers and RMMV's nw.js. I have converted most of the Luna Engine code base and think it looks more cleaner than the vanilla JavaScript version.


Secondly, writing your code in more structure way is good, it will help you a lot of problems in the future. For example, you always know the types of parameters (arguments) we pass to a function/method, and can easily find it to debug, while using vanilla JavaScript, you don't always find the type/class easily.


Thirdly, TypeScript is good for every traditional OOP lovers! I have seen a lot people coding JavaScript in Java/C# way, so why not try TypeScript.


What bad about TypeScript



TypeScript is good; however, there are still some problems with TypeScript.


Firstly, not all people want to learn a new language, even though TypeScript is mostly alike JavaScript (ES6). In real life jobs, many companies still prefer JavaScript than another superset. And if the RPG Maker community doesn't like TypeScript, you will be the only one who update the plugins!


Secondly, because of its structure and types, you have to manually define module, class, ... of third party JavaScript libraries so that the compiler won't raise any error in development. Luckily, most of popular libraries have a defination file for you. Sadly, RPG Maker MV library is not there.


Thirdly, the output JavaScript file might look ugly!


How to develop with TypeScript



Firstly, you have to learn the language itself. This will take very little time if you have been coding in JavaScript in a long time, and have a bit knowledge about OOP.


Secondly, you have to get the compiler. You can see the short tutorial in TypeScript homepage (http://www.typescriptlang.org/).


However, if the command-line is too hard for you, try Atom with atom-typescript, the code will be compiled by one press F6!


Please leave your opinions and comments ~
What are some problems that you have run into with plain old JS, that TypeScript solves?
The first reason is the syntax and notation, doing OOP with prototype and defineProperty is somewhat unnecessary longgggg.


And the second is: I love doing the OOP in more traditional way, with type-checking blah blah. 


The third reason is the ES5 itself, the ES6 is gonna be standardized in all browsers, and all ES6 => ES5 compilers don't fully support (If I remember correctly) all ES6 functions/methods... While TypeScript is a superset of JavaScript, and is a whole different language so it will be supported in a long term, and TypeScript is also popular now.


The additional reason is I want my library to be more structured, other reason is Angular 2, which I am learning for real life job.
It seems like the benefits of using TypeScript is only relevant if you're actually working with some substantial amount of code.


For example, a lot of plugins I write are probably 20-30 lines at best to solve certain problems, with one or two class definitions at most (usually none)


Would using typescript actually make it faster or cleaner, given that most of the time I'm simply working with existing methods and objects?
It depends, but for small plugins, plain JavaScript would be faster IMO. Although it would be left at personal taste.


I would not bother with TypeScript if I only touched existing methods and objects.
not that's I dislike your idea the problem is more like that for example if I want to edit you plugin I have to take typeScript library (if I do remember it's something you can install in your IDE) install it for being able to read a wannabe readable format.


not that it's bother me since I know of what you telling me it's a approach of C# approach since in C# you have to declare the variable type.


Although do they have good documentation? do if I input comment it's will destroy my comment on the output?
I'd recommend skipping TypeScript in favor of another transpiler: babel and flow. Babel allows you to write code in the next generation of Javascript, ES6 (and even ES7), and transpiles into ES5 syntax today. There's no weirdness to babel either, unlike CoffeeScript or TypeScript. Instead, you'll write ES6 Javascript and you're good to go. If type-checking is a desired feature, look into flow as well. Flow allows you to annotate your code in a natural JS Documentation fashion, and use this to provide type-checking for your code; the babel transpiler can throw warnings or errors if you are not meeting the type requirements.
@jamespedid  Actually the problem with Babel is yes you can write ES6 and even ES7 (even this not have the real use) it's just REALLY unreadable in the output.


what's Yami say I think is a approach of the Ruby and C# behavior because it's readable and shorter but also STILL readable when you output the format in JS okay it's ugly but it's kinda still readable and editable contrary to Babel who actually make the stuff to a core JS.


@Dr.Yami : I freaking remove what I just said TypeScript is freaking awesome. I love the C# feeling you have with typeScript.
Personally, i'm against using a different syntax (be it ES6, TypeScript or else) to write plugins, since you would need to provide all support by yourself. Most other scripters won't bother learning a new syntax or work their way through messy autogenerated code, when having to write compatibility patches.


ES5 OOP is actually pretty concise, if you throw in a helper function or 2, and is easier to read for other scripters.
@jamespedid Flow would do the same things as TypeScript with .js extension; however, as far as I know, the IDE supports for TypeScript are far far better, and the interface type is also good.


@Iavra Yes, that's the problem if people in community want to learn the new syntax or not. We might have to provide the support by ourselves if we go different approach. That's the reason why I didn't choose to use TypeScript at first. But well, writing in ES6 syntax is cool and somewhat shorter and cleaner, so I think it's also people taste which is matter there.
If you feel comfortable with TypeScript, i'd say go for it. Many threads have pointed out, that everyone has their own way of writing plugins, even though it's technically the same language. I feel comfortable writing ES5, but you might prefer a different way. Though, i personally wouldn't like to see TypeScript/ES6 in default plugins shipped with the engine, since it might confuse new scripters.
@Iavra yeah even with ES5, I have seen many ways of writing scripts too. I wonder if there is anyone in community go for functional lol. But yeah, for a big project, I think I will give TypeScript a try 😀
@Dr.Yami I've actually been pretty tempted to pick up TypeScript for RM, but have been experimenting with Babel as well (no flow yet, haven't figured that out, but I definitely want static typing). The lack of RPG definitions are keeping me away, but the static typing is certainly very appealing.

I'm honestly leaning towards using Babel on the premise that there should be less to learn with ES2015 in the long term- as you've mentioned before, TypeScript is a whole new language.
Is there an advantage to static typing in Javascript?
@Tsukihime yes, better debug in most cases. Also, it's about taste, since a lot people would love to do things in traditional OOP way ' v ')
Not sure how relevant it is to RM functionality, but in JS, type checking will contribute towards avoiding runtime errors, making errors easier to find early on, and improve code maintainability.
I'm late to the discussion but although I like the concept of Typescript I still think it's just like sweeping the negative aspects of JavaScript under the rug instead of actually learning how to write JavaScript better (in my case since I only started learning it two months ago).


For the community it would be better to outline some best practices or style guides so we can all write consistent code in a similar fashion, like how they would have at a large company.


After having programmed in python for 5 years it feels like JavaScript is a huge mess. Finding bugs can sometimes take ages when it would only take minutes in python. Reading other people's code can be a mess if it's written in a much different style. Lack of true OOP, privacy and types is a big problem in larger programs. JavaScript simply wasn't designed to handle anything other then simple client-side tasks and yet here we are writing huge programs in it. I don't think it's a bad thing I just hope the language evolves. I've avoided it for 20 years but I'm finally starting to feel like it's making some progress.


Anyways, that's my rant. I think TypeScript is not a good idea. The language itself needs to improve, shouldn't need to use a third party tool to do it. By the look of ES6 things will probably be a lot easier to do in the future so maybe we should just use babel like others have been saying. It's the future of the language.
I'm late to the discussion but although I like the concept of Typescript I still think it's just like sweeping the negative aspects of JavaScript under the rug instead of actually learning how to write JavaScript better (in my case since I only started learning it two months ago).


For the community it would be better to outline some best practices or style guides so we can all write consistent code in a similar fashion, like how they would have at a large company.


After having programmed in python for 5 years it feels like JavaScript is a huge mess. Finding bugs can sometimes take ages when it would only take minutes in python. Reading other people's code can be a mess if it's written in a much different style. Lack of true OOP, privacy and types is a big problem in larger programs. JavaScript simply wasn't designed to handle anything other then simple client-side tasks and yet here we are writing huge programs in it. I don't think it's a bad thing I just hope the language evolves. I've avoided it for 20 years but I'm finally starting to feel like it's making some progress.


Anyways, that's my rant. I think TypeScript is not a good idea. The language itself needs to improve, shouldn't need to use a third party tool to do it. By the look of ES6 things will probably be a lot easier to do in the future so maybe we should just use babel like others have been saying. It's the future of the language.

Like ES2015/Babel, TypeScript is a superset of of JavaScript, so everything we already have in JavaScript is also TypeScript- most of what it adds are things you're also going to find in Babel- mainly the class-based programming. Down the line, TypeScript is actually going to be a superset of ES2015, and in fact, the TypeScript compiler supports an ES6 target.

With TS, we're not necessarily sweeping the negative aspects of JavaScript under the rug, but we're swapping out a lot of the key/good aspects of JavaScript that work well in a smaller client environment for abstractions that enable a more traditional OOP approach to solving programs- this in terms makes it easier to write larger codebases. Typescript adds static typing support, for example, which is absent from ES2015. TS also gives you access to abstractions that enable the use of interfaces, enums, and generics. The key term here is 'abstraction'. Everything we do in TS is possible in JS, TS just bundles those abstractions into the compiler and links them to syntactic sugar as a convenience feature to make it quicker to write solutions.
I haven't read into it too much but it seems like you can use plain javascript as typescript and it will compile just fine, but that it also adds some extra stuff to make things easier.


Do you know if typescript supports private member variables? I've found it to be a headache sharing data with the class but not the instances in javascript. I don't think naming a variable _myvar is enough to deter people from assigning data directly to it when they should be using the getter/setter. I know this because I use to do this exact thing 2 months ago before I knew javascript very well.
I haven't read into it too much but it seems like you can use plain javascript as typescript and it will compile just fine, but that it also adds some extra stuff to make things easier.


Do you know if typescript supports private member variables? I've found it to be a headache sharing data with the class but not the instances in javascript. I don't think naming a variable _myvar is enough to deter people from assigning data directly to it when they should be using the getter/setter. I know this because I use to do this exact thing 2 months ago before I knew javascript very well.

Typescript is public by default, but supports private and protected members. Obviously, we don't derive major benefits from this when we transpile back into ES5, but it other developers are consuming your TS code/definitions, it permits some level of encapsulation.

Original thread: https://forums.rpgmakerweb.com/threads/typescript-for-plugins-development.56963/