Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// 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"});

js-n commented Oct 2, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment