TypeScript - split to multiple files

0
Let me preface my question by saying that I realize this is more of a generic StackOverflow question, but I'd like to get a response here, both because the versions of TypeScript and Node.js are known and for future reference. I tried splitting my single script.ts file into three files - I've tried both the namespace approach and the module approach - but either compilation or execution fails. Neither the TypeScript handbook, StackOverflow nor Google helped me solve the problem. Does anyone have a step by step guide to split your script.ts file into multiple separate files? [edit] These are the errors I get when I run my script with modules: D:\Workspace SDK\CreateWS>node script.js D:\Workspace SDK\CreateWS\momdata.ts:1 (function (exports, require, module, __filename, __dirname) { export class Entit y { ^^^^^^ SyntaxError: Unexpected reserved word at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object.<anonymous> (D:\Workspace SDK\CreateWS\script.js:5:12) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) and D:\Workspace SDK\CreateWS>node --harmony_modules script.js D:\Workspace SDK\CreateWS\momdata.ts:1 (function (exports, require, module, __filename, __dirname) { export class Entit y { ^^^^^^ SyntaxError: Unexpected token export at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object.<anonymous> (D:\Workspace SDK\CreateWS\script.js:5:12) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) [Edit2] I solved my problem: I noticed in the error message that a .ts file was referenced. I manually edited the generated script.js files to point to the momdata.js file, instead of momdata.ts file. This then leads to another question: what should I do to automatically convert the line import data = require("./momdata.ts"); to import data = require("./momdata.js"); ?
asked
3 answers
6

You don't really have to explicitly type the extension, just require('./x') without the '.ts' and node will pick it up then.

answered
1

This chapter from Typescript Deep Dive book is also relevant:

answered
0

What error do you get when you try to use modules? Both approaches should work.

answered