This project is read-only.

Variable type definitions

Topics: Language Specification
Jul 2, 2013 at 2:45 PM
While checking out the tutorial page I stumbled across this:
interface Person {
    firstname: string;
    lastname: string;

function greeter(person : Person) {
    return "Hello, " + person.firstname + " " + person.lastname;
So I wonder, why not simply stick to variable definition as it has always been stated?
function greeter( Person person ) {
    return "Hello, " + person.firstname + " " + person.lastname;

var Admin = { firstname: "Sr", lastname: "Admin" };
function greeter( Person person = Admin ) {
    return "Hello, " + person.firstname + " " + person.lastname;
Any thoughts?
Jul 2, 2013 at 5:52 PM
Certainly this was considered (everyone on the team has a strong C# background and we have been known to accidently write type-first code on the whiteboard).

The main problem is that we needed to add types in a way that felt natural with the existing JavaScript syntax. It's not clear how you would add type annotations to function return types (especially for function expressions) or var declarations. If you look at other OO/functional languages like F# and Scala, you'll see they use very similar syntax.
Jul 2, 2013 at 7:28 PM
:-/ well what feels natural to me is type-first annotation, anyways the effort is pulling different sides and by that I mean that either TypeScript stays close (and constrained) to javascript "look and feel" syntax or it keeps enhancing javascript by adding whatever is needed as long as it is done in a neat and elegant way.

That's a question that could/should be asked to the community if they prefer "function greeter(person : Person)" vs "function greeter( Person person )" ^_^

On a side note, I'm planning to rewrite the major version of my JavaScript Framework using TypeScript and I don't mind if later I have to rewrite the whole thing with the type-first annotation.

Thanks for your quick response Ryan :)