Type Casting JavaScript with Typescript
Learn how TypeScript adds type safety to JavaScript. Explore type casting, tooling benefits, and why TypeScript differs from CoffeeScript for enterprise development.
Whenever something came from Microsoft or a Microsoft employee, there is always a buzz… And I still don't understand why a person's company policies have anything to do with a person's attitude or approach. The same happens when TypeScript came. It was just a day old and open source people started bashing it like anything. Comparing it with CoffeeScript, Dart. Guys, it is also open source and Microsoft in any case is not forcing anything. As of now, if you like it, use it; if you don't, just don't use it.
In any case, it is just JavaScript. There was a very positive buzz when CoffeeScript came. But for TypeScript, it was not that fortunate.
It is normal. Even I thought it was just another thing coming from Microsoft that is similar to CoffeeScript. But I was wrong—it is far different from CoffeeScript. I have a long-time relationship with typed languages like C-*, Java, and recently fell in love with Ruby-like languages. So, I kind of like both. I don't have a problem with any of them. Whenever I am learning Ruby on Rails, I like to code with CoffeeScript, and even I started using it with my day-to-day work in .Net. But there is no symmetry in that. I am missing that. And TypeScript came to solve that issue. Obviously, some people claim that they don't need good tools, but I still wonder how a person can make enterprise-level language using Notepad.
Check out the comments section over this blog:
TypeScript just provides types to JavaScript. And with that, it provides tooling support for writing client-side code. Nothing else. Even it is not trying to bring the good parts of JavaScript to you, like CoffeeScript does. Not forcing anything and also not doing any magic behind the scenes. So, you can say it is very close to the metal, pure JavaScript. And I like and also dislike this thing. I like it because it allows me to copy-paste, and I dislike it because it also allows bad parts of JavaScript to make it into production code. I personally like CoffeeScript for the reason that it doesn't allow any bad parts but restricts/forces you to write good parts only. Again, this is a personal view and preference. I am not that good with JavaScript, so I prefer writing code in CoffeeScript, which takes care of everything else and emits beautiful JavaScript.
But a person who is good with JavaScript or is believed to be good with JavaScript—TypeScript is for them. It is a wonderful thing which provides complete freedom of JavaScript with types to help you out.
So, let's check out what the hell TypeScript is.
I have a special relationship with this. Because this is the first language I have read the language specification for. Huff, it's nasty. At first glance, I haven't got a single thing. But then when I got to the state of understanding, it's just wow. I just loved it. I should have read the language specification with Pro Books. I personally insist on visiting the site and checking out TypeScript; a wonderful introduction video is there. And lots of samples are also available too. Also, there is a specification available. It is nice, must-read material.
If you are a lazy genius, then you should continue reading. I will try to cover as much as possible of TypeScript.
Basically, you can just throw JavaScript into TypeScript just like that. It will work because of the dynamic data type "any". But just like the dynamic keyword of C#, it will not provide IntelliSense support. But I think it's fine if you're using some jQuery plug-in just to do some UI effect or something like that.
TypeScript has the module keyword to define a namespace. In that module, you can throw classes, functions, interfaces, or variables. If any of those are not exported or public in the case of functions, then that is private to that module. And the module is a public/global variable. So, everything within that is exported using the module pattern. Here is a sample directly taken from the TypeScript site.
module Sayings {
export class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
}
var greeter = new Sayings.Greeter("world");
var button = document.createElement('button');
button.innerText = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);
That will generate JavaScript as shown below.
var Sayings;
(function(Sayings) {
var Greeter = (function() {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function() {
return "Hello, " + this.greeting;
};
return Greeter;
})();
Sayings.Greeter = Greeter;
})(Sayings || (Sayings = {}));
var greeter = new Sayings.Greeter("world");
var button = document.createElement('button');
button.innerText = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
};
document.body.appendChild(button);
Lovely, I guess. So, this is basic TypeScript; it's nothing but JavaScript with classes. So, I won't copy-paste more examples here, but I'll talk about a few things I like about TypeScript. And you can always visit the site to check out more. If you have read my CoffeeScript article, it was longgggggg… but in that, there is a lot to cover. If I started writing that much about TypeScript, it will be JavaScript classes only.
So, here is a small list of things I like about TypeScript:
- It provides support for number, string, array, and dynamic function types
- Easy to integrate with external JavaScript libraries
- One can create or export declaration files to provide IntelliSense support
- Modules, classes, and private/public functions are just super awesome as it feels like a typed language
- Interface support is there, just another addition to a dynamic language to make programming feel like anything
- jQuery, jQueryUI, and Win8 files are available in samples. I guess the best way to learn jQuery is to fire a command and be sure that you will not get spelling mistakes
- AMD and module pattern are default in the build, so classes are ready to use with frameworks like RequireJS. // I seriously want this with CoffeeScript
- The miserable "this" keyword is handled nicely
- Very little change in your written functions and generated JavaScript functions. // At some point, I don't like this as it allows bad parts in your functions
Obviously, I don't like a few things about TypeScript too.
- It is allowing bad code; even comparison operators are not getting corrected like other counterparts
- Easy to go wrong as it provides free will to step outside the boundary without forcing you to stay within it
- It is not as sweet as CoffeeScript
Comments and corrections are always welcome. But I strongly recommend not bashing the company or the new language. I am not working at Microsoft, but we are living in a free world, and TypeScript is open source under the Apache license.
Frequently Asked Questions
TypeScript is a typed superset of JavaScript that adds static type checking and tooling support for writing client-side code. Unlike CoffeeScript, TypeScript stays very close to pure JavaScript, allowing developers to gradually adopt types without forcing them to rewrite their code or change how JavaScript fundamentally works.
TypeScript provides types and tooling support that help catch errors during development rather than at runtime, making it ideal for enterprise-level applications and large codebases. It's particularly beneficial for developers who are comfortable with typed languages like Java or C# and want the safety of type checking while writing JavaScript.
TypeScript and CoffeeScript serve different philosophies. CoffeeScript restricts bad JavaScript practices and forces you to write good code, while TypeScript gives you complete freedom of JavaScript with types as optional guidance. The choice depends on your preference: CoffeeScript for enforced best practices, TypeScript for flexibility with type safety.
TypeScript is specifically designed for developers who are good with JavaScript or want to write JavaScript with type safety. However, beginners might find CoffeeScript more forgiving since it automatically prevents common JavaScript pitfalls, while TypeScript allows both good and bad JavaScript practices to coexist.