简体   繁体   中英

Is it possible to reference typescript files in other projects without adding projects to the solution?

In large scale software, it's a good idea to break code into projects. For example, have a framework project which contains all base classes in some project called Company.Framework and some other projects which uses those shared codes like Company.ProductA, Company.ProductB .

Is it possible to reference .ts files in other projects, for example just referencing its dll , not adding the project, so the framework project can be hidden from the business developers.

The question is how to reference .ts files in other projects without adding those projects to the solution. For example just by adding their dlls .

I'm sure you know this, but Typescript files (.TS) get compiled to Javascript files (.JS). Neither the Typescript files nor the generated Javascript files end up inside the DLL. The DLL contains server-side code only, and the Typescript/Javascript is client-side code.

So, trying to add a reference from your ProductA project to the DLL from the Framework project is not going to pull in any Typescript files that were in your Framework project.

As long as the final rendered HTML page includes the tags for both the generated Javascript from both ProjectA as well as Framework, then everything will work fine, even if the two projects have no connection between them. If what you're really after is Visual Studio Intellisense for the Framework classes while you're coding in Typescript in ProjectA, then you should do as @WedneyYuri and @DavidSherret suggested, which is to add the .d.ts file from your Framework project into your ProjectA project.

Managing Dependencies

The simplest solution is to use a package manager like bower :

On terminal: Create an empty folder and start an empty bower repository:

$ bower init

For each definition file use this command:

$ bower install [URL] --save-dev

Example for jQuery definition file :

$ bower install https://raw.githubusercontent.com/borisyankov/DefinitelyTyped/master/jquery/jquery.d.ts --save-dev

Thanks to the bower you can use any URL here. For more information visit the bower documentation .

Compilation

Create a file (name isn't important) definitions.d.ts in the same folder where you ran the command $ bower init .

For each installed definition file you must add manually a new line in this file:

example after adding jQuery and angularjs :

/// <reference path="jquery.d/index.ts" />
/// <reference path="angular.d/index.ts" />

Now, in your project you only need to include references to the definitions.d.ts file

Running the code

For the code to work you need the .js files. In development environment I use the bower to manage the dependencies in a separate folder and I manually add the .js files in html. In production use any tool to concatenate them.

TSD (TypeScript Definition manager for DefinitelyTyped)

In the near future the package manager will have option to install modules from anywhere. It's not ready, but it is a goal.

The goal of TSD future is to seamlessly support any TypeScript definition files, from any source. By default, you should still be able to search the definitely typed repository and install from there. However, more focus will be trained onto support independent definitions, specifically ones bundled with modules. https://github.com/DefinitelyTyped/tsd/issues/150

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM