You have a internal vs external modules problem.
The TypeScript Collections library is written as an internal module -- a standard JavaScript file that you could just throw in to a script
tag in a webpage.
Node's require
, however, is expecting an CommonJS-compatible file that will assign something to exports
, in other words an external module. What's happening is that node.js finds collections.js
, executes it, and returns you back the exports
object from evaluating the file. Because it's just a regular JS file, the exported object is {}
-- empty.
The best fix would be:
- Replace your reference to
collections.ts
with one tocollections.d.ts
, just for correctness's sake (runtsc --d collection.ts
to generate this file) - Use some solution for loading "vanilla" JS files in node. A good one-liner (from the linked question) is
eval(require('fs').readFileSync('./path/to/file.js', 'utf8'));