I know this is an old question, but recently I ran into a similar problem that could not be solved in this way, since I had to return an empty array of a certain type.
I had
return [];
where [] was Criminal[] .
Neither for me nor for return: Criminal[] []; , nor for return []: Criminal[]; .
At first glance, I solved this by creating a typed variable (as you correctly reported) before returning it, but (I don’t know how JavaScript engines work), it can create overhead and are less readable.
For thoroughness, I will also report this decision in my answer:
let temp: Criminal[] = []; return temp;
In the end, I found the TypeScript type casting, which allowed me to solve the problem in a more concise and readable (and possibly efficient) way:
return <Criminal[]>[];
Hope this helps future readers!
Fylax Aug 09 '16 at 8:46 2016-08-09 08:46
source share