Archive of ‘JavaScript’ category

Global variables in TypeScript

A caveat for when you’re declaring global variables (in this case it was moment JS) – if you declare a global you’re defining elsewhere (e.g. in a library) at the top of a file like this:

var moment: MomentStatic;

The compiler etc won’t complain (it’s a valid use case) but it can break your code in IE8 and below (it adds an enumerable but undefined property to window…). Make sure to use declare var:

declare var moment: MomentStatic;

This will remove the “var moment;” from the compiled JavaScript and thus avoid declaring a duplicate variable