Within TypeScript, the any
type allows for casting to and from any arbitrary type. For example, you can cast from a variable of type any
to a variable of type MyArbitraryType
like so:
var myThing: MyArbitraryType;
var anyThing: any;
myThing = anyThing; // implicit cast from 'any' to 'MyArbitraryType'
//or
myThing = <MyArbitraryType>anyThing; // explicit cast from 'any' to 'MyArbitraryType'
However, attempting to cast from an array of any[]
to a MyArbitraryType[]
results in an error:
var myThings: MyArbitraryType[];
var anyThings: any[];
myThings = anyThings; //implicit cast from 'any[]' to 'MyArbitraryType[]'
// or
myThings = <MyArbitraryType[]>anyThings; //explicit cast from 'any[]' to 'MyArbitraryType[]'
Interestingly enough, using any
as an intermediary enables successful casting between the two array types:
myThings = <MyArbitraryType[]><any>anyThings;
While this workaround functions, it may seem somewhat clumsy. The question arises - why is any
easily castable with various types, but not when dealing with arrays? Is this limitation an oversight within TypeScript, or does there exist another syntax that could address this issue?