0

Besides the obvious (hah).

I'm still no sure why you would choose either of those.

I used to think compiling a single file was easier, as you use <reference>s to link files and can use nested modules to manage a global namespace.

But now I'm thinking that import/require and many output files might be good for greater modularity (and similarity to regular JavaScript / ES6).

But can you namespace types adequately using imports? Using extra level of modules seems awkward when importing.

And won't we still need <references> to resolve the type annotations?

How do I decide on this?

3
  • 1
    Have you read this? It covers most of your questions typescript.codeplex.com/… Commented Feb 26, 2014 at 4:33
  • just thought I'd share this as well ;) youtube.com/watch?v=KDrWLMUY0R0&hd=1 Commented Feb 26, 2014 at 8:02
  • Thanks for the link to the manual, I wasn't even aware it existed! Digging into it right now. Commented Feb 26, 2014 at 15:29

1 Answer 1

1

I used to think compiling a single file was easier, as you use <reference>s to link files and can use nested modules to manage a global namespace.

Basically using --out:

Pro: Simple to begin with. Types are importable via simple <reference comments.

Con: Single file. Not easy to debug if you debug JS.

But can you namespace types adequately using imports?

You should not use internal modules when you are using external modules. For external modules (amd/commonjs) each file is its own module. That will avoid that "extra / unnecessary" level of indirection.

And won't we still need to resolve the type annotations?

No. As soon as you export something at the root level ///<referenceing that file no longer does anything. The only way to import types (even interfaces) from such a file is to use import/require

Note:

To overcome to get the pros of --out i.e easy type import via ///<reference + still allow simpler debugging of JS, grunt-ts supports generating an amd-loader for you: https://github.com/grunt-ts/grunt-ts#advantage-of-using-amdloader-option

Sign up to request clarification or add additional context in comments.

4 Comments

For import/require I fear creating unresolvable dependency loops, but worth a try. The amdLoader sound good (I'm still hazy on the practical details), but could it also work for commonjs (node)?
could it also work for commonjs (node) it will need to be different implementation. And tricky since it is best to use import/require.
I fear creating unresolvable dependency loop : if a needs b and b needs a, and a is the first to execute, a gets b as null and needs to handle it
For values it is solvable, but with import it gets wonky if you want to declare types: if A has a method that returns a B, and B has a method that returns an A, then import ordering would be problematic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.