I'm a typescript newbie, I'm picking up an existing durandal project to make some changes. I've noticed that this code snippet gives a runtime error.
declare var routeLinks: IRouteLinks;
routeLinks = new RouteLinks();
export = routeLinks;
Error = Failed to load root module (ui/shell). Details: routeLinks is not defined(…)
When I change the code to remove the declare
keyword (below) however, the issue goes away and the code seems to work as intended.
var routeLinks: IRouteLinks;
routeLinks = new RouteLinks();
export = routeLinks;
This pattern is replicated in a number of places, each time I get a runtime exception I go in and remove the declare
keyword and it fixes the issue.
- Can anyone explain why including the
declare
keyword is causing this issue, and why removing it fixes it? - What was the possible intention of using the
declare
in this context the first place (in newbie terms if possible)? - And if this code was previously working, how come on my box it fails? (breaking changes between typescript or durandal versions maybe?)
Thanks.
Further Info
Looking in to this further, when I compare the compiled js i see this line is missing when we use the declare
keyword
var routeLinks;
So the error now makes more sense to me, but why does using declare
cause the cross-compiler to drop the variable declaration, this seems counter-intuitive.
declare
means that the variablerouteLinks
is already defined somewhere else and that it should be loaded in the environment before loading this code snippet. When you remove thedeclare
it actually outputs the variable declaration to the compiled js. I'm not sure how you load things so it's hard to answer more specifically. – Ollie