简体   繁体   中英

Typescript split solution into several projects

I used ScriptSharp before it was frozen. Since TypeScript is a developing OOP language I decided to try it. I use visual studio (if it matters). I have troubles making simple things I used to do in ScriptSharp. I didn't expect it would be that difficult. What I want to do:

  • Create project A (Class Library Project) with module AssemblyA. AssemblyA module will have some exported classes.
  • Create project B (Class Library Project) with module AssemblyB. AssemblyB will reference AssemblyA types and use them as parameter types and etc.

Can you give me some guide how to make it work or sample? Thanks.

UPDATE: What's for I can add reference to another typescript project? It would be great if output of referenced project was copied to that project.

Rather than having assemblies and modules, you have modules that can be organised into namespace-like hierarchies:

Internal Modules

Internal Module Example:

module AssemblyA {
    export module ModuleA {
        export class Example {

        }
    }   

    export module ModuleB {
        export class Example {

        }
    }
}

var x = new AssemblyA.ModuleA.Example();
var y = new AssemblyA.ModuleB.Example();

You can also define these internal modules across multiple files...

modulea.ts

module AssemblyA {
    export module ModuleA {
        export class Example {

        }
    }   
}

moduleb.ts

///<reference path="./modulea.ts" />
module AssemblyA {  
    export module ModuleB {
        export class Example {

        }
    }
}

app.ts

///<reference path="./modulea.ts" />
///<reference path="./moduleb.ts" />
var x = new AssemblyA.ModuleA.Example();
var y = new AssemblyA.ModuleB.Example();

External Modules

And if you want to write really large applications, you can use external modules (where the file represents the module).

assemblya/modulea.ts

    export class Example {

    }

assemblya/moduleb.ts

    export class Example {

    }

app.ts

import ModuleA = require('./assemblya/modulea');
import ModuleA = require('./assemblya/modulea');
var x = new ModuleA.Example();
var y = new ModuleB.Example();

I found a workaround for my problem:

In project AssemblyA:

  1. Specify "Combine javascript output into file" to "..\\AssemblyB\\AssemblyA.js".
  2. Set up Generate Declaration files into true.

In project AssemblyB:

  1. Add reference for intellisense in app.ts ///<reference path="../AssemblyA/AssemblyA.d.ts" />
  2. Add reference to generated file in html: <script src="AssemblyA.js"></script>

In project B you can use any namespace aliases (for example: import AssemblyANS2 = AssemblyA.NS2;) or fully qualified name. Put classes in different files, Use same module name and there is no need to refer to ts files.

What I didn't like is that referencing project doesn't make any sense, but I wanted steps 1-2-3-4 to be done automatically after adding reference. Also "Redirect javascript output to directory" setting doesn't work when "Combine javascript output into one file" is specified. It's also weird that I can specify file path in second options. I expected these settings to be combined with Path.Combine. Maybe my solution is not ideal, but it's exactly what I need. Feel free to suggest better idea.

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