Why the backwards colon parameter typing?

Topics: General
Oct 1, 2012 at 7:30 PM

With javascript so similar to C++ and C#, why the decision to come up with

(person: string)


(string person)


Oct 1, 2012 at 7:47 PM

The x:T style is quite popular in non-C-style languages, but yes it is a little unusual in a curly-brace kind of setting. We chose it because it is less intrusive on the underlying JS syntax. Prefix types - "T x" style - work great when types are mandatory. But in TypeScript they are optional, and colon-style type annotations lend themselves better to "adding as needed".

Note that TypeScript does quite a bit of type inference so omitting types syntactically doesn't necessarily mean "untyped." For instance, if the return type is omitted on a function, TypeScript will try to infer it from return statements. That way the amount of type annotations you need to add to a JavaScript program to make it strongly typed is usually quite small.

Hope that explains it!

Oct 2, 2012 at 2:41 AM

Another important reason is that ECMAScript 6 will be introducing the "let" keyword (which will have block scope and essentially replace "var"). If you declared variables using only the type, it would be ambiguous as to whether the compiler should translate that to "var" or to "let".

Oct 3, 2012 at 11:19 PM
Edited Oct 3, 2012 at 11:20 PM

Another similar question. Why only one ":"? Previous guards/trademarks proposals used "::" like ES4-ActionScript3 which made it more obvious that something is a type annotation and not part of an object declaration. This will get a bit ambiguous when we get object destructuring into TypeScript. For example

function ({ foo: x, bar: y }) {
  return x + y;

In TypeScript my guess is that people will combine destructuring and types in parameters like this:

function ({x, y}:SomeInterface) {
  return x + y;

But it's still potentially ambiguous.

Dec 27, 2012 at 3:28 PM

Why  not use an syntax compatible with ES8 guards proposal x :: T ? This will align typescrit with TC39 proposals.... 

Dec 27, 2012 at 5:03 PM

I always thought that the guards proposal is all about run-time type checking and not static type checking. So the TypeScript compiler could translate x:T into x::T when the JS code is generated for target ES8, but other than that the idea behind it is very different (at least that was my understanding).

BTW, has the guards proposal already been pushed back to ES8? What is the estimated time of arrival of that, 2018 ???