Array and Object Literal Types

Brendan Eich brendan at
Fri Aug 17 15:22:10 PDT 2007

On Aug 17, 2007, at 2:47 PM, Garrett Smith wrote:

> It would be nice to have Array literal syntax declare it's ArrayType
> before the declaration of the array.
> For example:
> 1. var x : Array<String> = [ "foo", "bar", document.title,  
> getAnotherString() ];

Array is not a parameterized type. If it were, the syntax would be  

Array must be backward compatible, so if it were changed to be  
parameterized by element type, then it would have to default to  
Array.<*> when used without a parameter list. But we don't have  
default type parameters, and anyway a type-parametric Array reference  
without the actual type param list would be ambiguous: should it mean  
Array.<*> by default, or Array, with instantiation using an actual  
type param happening later: let A = Array; type TA = A.<T> ?

So there's no good way to overload Array as you want.

Also, Array is closer to Object than to any homogenous tuple or  
vector type. Hence the separate


> 2. var x = [ "foo", "bar", document.title, getAnotherString() ] :  
> String;
> example 1 is more clear to read because in a long array, you'd see
> right away what type of Array it is.

Please see the above proposals and the prior proposals:

> Is the former syntax (or some variant thereof) going to be available?

No, as explained above. But what you might want is:

let x : [string] = ["foo", "bar", document.title, getAnotherString()];

* let trumps var in any hand ;-)
* string is the type of "foo", String is the dynamic class for  
wrappers, as in ES1-3 that shares its .prototype with string  
instances as well as String instances, via the usual prototype-based  
delegation. Prefer string in general.
* [string] is a type expression denoted a structural array type.

> What about for Objects?

See structural object types.


More information about the Es4-discuss mailing list