public
Created

Typescript?

  • Download Gist
typescript_or_js.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// an example of TypeScript "interfaces"
interface Person {
firstname: string;
lastname: string;
}
 
function greeter(person : Person) {
return "Hello, " + person.firstname + " " + person.lastname;
}
 
var user = {firstname: "Jane", lastname: "User"};
 
document.body.innerHTML = greeter(user);
 
// some idiomatic JS
function greeter(args) {
var first = args.first || "Guest";
var last = args.last || "";
return "Hello, " + first + " " + last;
}
document.body.innerHTML = greeter({first : "Joe", last : "blow"});

TypeScript doesn't help in a 21-line example (although it doesn't get in your way, either). You could inline the default arguments to make it looks a little nicer:

function greeter(first = 'Guest', last = '') {
  return 'Hello, ' + first + ' ' + last;
}

I'm skeptical of the class system (although it's largely based on the work of TC39 for ES6), but I really really like the implicit structural interfaces.

What if I want to rename the firstname attribute of the Person class to firstName? Simple, replace all in all files, right? Now imagine I have an application with 2 classes that have that exact same attribute. Now how do I rename it? Humans inevitably make mistakes doing things like this and then have to rely on 100% test coverage prior to the code failing in production. Now imagine the headache when you take over someone else's software that you didn't write and they have 10, 20 or more classes that have the same name (believe me it happens). Static typing can drastically improve productivity when dealing with large applications when there are static references. I can't tell you how much I hate the introduction of the var keyword in C#. Too many people use it, when they don't need to, instead of declaring the type. This makes analyzing and/or refactoring an application extremely difficult down the line.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.