Can you create Typescript packages? like c # dlls

Is there any concept of packaging typescript files now?

One thing that I find at the moment when I try to transfer a clean javascript project to typescript is links, in some cases when I have complex objects, I have to write several help statements that pull files from all sides.

This partly depends on my layout of the project, as it is quite large and modular, so I have such a system:

- modules |- module1 |- models |- services |- controllers |- module2 |- models |- services |- controllers |- core |- models |- services |- data |- validation 

There is much more, but you understand, now the kernel is used by each module, but with javascript I just expect it to be loaded at runtime, which should happen anyway, however, as typescript problems really only happen at compile time. I was wondering if there was any idea about packing all typescript files into any typescript library or something like that, and then they can be referenced from projects, not module 1 models, referencing the main models and etc.

Currently, the problem is with directory structures, as namespaces work fine, but if I move the file, I need to go to each file that refers to what moves the file and updates it. This is tedious, whereas if there is any idea of ​​a package, I could just refer to it after its release, so I no longer worry about file systems and directories, I just worry about the package and namespaces.

I think this is very similar to how C # works, you have a project with links. Then, each file within this project can use any of the classes in links, so the impact of the code is controlled by links and namespaces.

I think that my build script just creates a local reference.ts file and simply iterates over each * .ts file in the relavant module and puts them in one big file:

 ///<reference path="core/models/some-model.ts"/> ///<reference path="core/models/some-model-2.ts"/> ///<reference path="core/services/some-service.ts"/> 

as shown above, and then use this link file in all typescript files that require core files, so it acts as a kind of link at the project level, this may mean that some files have links that they don’t need, but its time compilation I don't care ...

I do not want to apply my solution to this problem, if a good way already exists, I hope this makes sense ...

== EDIT ==

I just wanted to post it here, since my script saved me TONS of time, and also reduced my link guff by about 99%, this is not applicable for people who don't have build scripts.

Now, assuming you have a script assembly, I took the path to take a step in my script that went through each individual file in the root directory (module1, module2, etc. in this case), and this then output the local.references.ts file to the help folder in this directory. Then I manually wrote the external.references.ts file, which refers to external descriptors or other module references wherever needed.

After this part has been done, when I compile my typescript, I basically list it in the root directories and tell them that they compile them all (* /. Ts) into one large js file (e.g. module1.js). Now, since this will automatically include local and external links, I don’t need to put ANY links in separate clans.

Thus, this way of providing local and external link files (local.reference.ts, external.reference.ts) is included in the bulk processing of files that you just need to worry about namespaces, which makes it almost the same as C # will work.

If, however, you don't have a script assembly that can do your local link building and typescript compilation, then a comment link would be a good option.

+4
source share
1 answer

There is currently no formal process for packaging typescript source files as a pre-built library, as you describe.

Currently, several different solutions are being used, similar to those that are related in the comments, which will allow you to place all your links in central typescript files, and then just link to them from your individual scripts or to the approach that you put forward where you are executing the same approach, but instead of manually writing it, you get your build script to create links for you and get the compilation process to insert links, and not to directly link to them in each file.

As typescript becomes more mature, there may be more formal ways to do this, but for now, just take any solution that works best for your toolkit and evaluation for development with Typescript.

+1
source

All Articles