Multiple files making up Type Script project

后端 未结 3 1177
不知归路
不知归路 2020-12-31 00:15

Recently I have been working with TypeScript and everything has been fine and I really, really like it.. Makes JavaScript workable again! :)

we have tried to follow

相关标签:
3条回答
  • 2020-12-31 00:55

    I wrote about getting the right set up for TypeScript and one of the ideas in there will help you - I originally got the idea from Mark Rendle.

    The idea is that create a file named references.ts and have all of your dependencies listed in there. A bit like this:

    /// <reference path="modulea.ts" />
    /// <reference path="moduleb.ts" />
    /// <reference path="modulec.ts" />
    /// <reference path="moduled.ts" />
    /// <reference path="modulee.ts" />
    

    You can then simply reference this file at the top of all your TypeScript files:

    /// <reference path="references.ts" />
    module ModuleA {
        export class MyClass {
            doSomething() {
                return 'Hello';
            }
            tester() {
                var x = new ModuleB.MyClass();
            }
        }
    }
    

    The references.ts file acts like the References folder you have for a .NET project.

    The other recommendation, which works quite well with this set up, is to use the --out flag to compile a single JavaScript file, starting with app.ts. The TypeScript compiler walks all the dependencies and works out the correct order for all the generated JavaScript.

    tsc --out final.js app.ts
    

    This strategy will scale with your program much better than manually referencing specific files everywhere.

    0 讨论(0)
  • 2020-12-31 00:57

    If you come like me from a C++ / C background and miss the .h (or .hpp) files, I have a way. And you don't have to change compiler flags, or use ///, or anything. Just plain simple .ts files, import, export.

    You create a folder, name it "models"

    In this folder, you can create your models, say:

    automobile.ts

    export class Automobile {...}
    

    car.ts

    import { Automobile } from './automobile.ts';
    
    export class Car extends Automobile {...}
    

    ferrari.ts

    import { Automobile } from './automobile.ts';
    import { Car } from './car.ts';
    
    export class Ferrari extends Car {...}
    

    and then the magic - header files!

    you create a file called models.ts (could be header.ts, whatever) in the same folder, where you just do

    export { Automobile } from './automobile.ts';
    export { Car } from './car.ts';    
    export { Ferrari } from './ferrari.ts';
    

    now, in your other files, to import all your models

    import * as x from 'src/app/models/models';
    

    and be happy using

    let myCar: x.Car = new x.Car();
    let myFerrari: x.Ferrari = new x.Ferrari();
    
    0 讨论(0)
  • 2020-12-31 01:04

    You have two options here, either commonjs, or AMD (asynchronous module loading).
    Using commonjs, you will need to place a

    <script type="text/javascript" src="lib/jquery-1.7.2.js"></script>
    

    for each separate javascript file in your project in your html page.
    Obviously this can become very tedious, especially if you have hundreds of files in your project.
    Alternatively, as Steve Fenton pointed out, you can use the --out parameter to compile various files into a single file, and then reference just this single file in your html page.

    Using AMD is the third option - where you only need to include one script tag in your html page, and AMD takes care of references.
    Have a look at my blog on how to get this working : http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/

    0 讨论(0)
提交回复
热议问题