Thursday, October 11, 2012

TypeScript's interface looks like F#'s record

TypeScript is a superset of ECMAScript5 by Microsoft. And Microsoft is aiming to make some TypeScript syntaxes as ECMAScript6 specification (check the specification document). Like as coffee script syntax is similar to Ruby syntax, TypeScript syntax is similar to C# syntax. But, its interface syntax is similar to F# syntax, not C# one.

TypeScript's interface
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);

F#'s record
type Person =
    { firstName : string;
      lastName : string }

let greeter (person:Person) = sprintf "Hello, %s %s" person.firstName person.lastName

let user = {firstName = "Jane"; lastName = "User"}

greeter user |> System.Console.WriteLine

Of course, these are entirely different classses and types. These just looks like similar.

No comments:

Post a Comment