Mixed type Arrays declarations not allowed?

Topics: Language Specification
Apr 18, 2013 at 8:56 AM
I expected the following to compile:
var declarativeTree = [ 1, [ 2 ]];
var proceduralTree = [];
But the compiler issues an error for the declarative case saying it cannot infer the array type. The procedural case works fine.

Apr 18, 2013 at 3:01 PM
You can declare your array to contain any type and then it's ok with that:
var declarativeTree: any[] = [1, [2]];
Apr 18, 2013 at 3:06 PM
It does seem like the compiler could infer the "any" type for mixed-type array literals; maybe generating a warning for times when you've done it by mistake.
Apr 18, 2013 at 4:36 PM
Ah! Thanks. I was just groping for the correct syntax. Such a newbee.
Apr 19, 2013 at 8:20 AM
Unfortunetly the team doesn't seem keen on fixing issues like this.

There has been many issues created that ask's to treat more things as Any as soon as it can't be treated as otherwise, but it's always rejected with "It's by Design"...

I just happen to think that this is a bad design decision, as when we try to run existing large projects through typescript, this is one of the biggest error generators, and it should be clear that these things appear to be common practice on javascript projects, since large projects like Angular, Ember, jQuery, Backbone, Grunt and so forth all generates errors that could be avoided with a more lose approach to typing.

Personally I would rather have that everything was treated as any, unless I specifically asked for it to be something... Than everything being restricted to it's first declaration...
Apr 19, 2013 at 4:52 PM
Seems like a bug to be. TypeScript failed to infer the correct type. The correct type is Any. If that is not what the developer wanted she'd find out soon enough because her intellisense for the array would be typed as any and she wouldn't get any of the properties of the type she was expecting on the element types. If anything this should be a warning.