How do I import other TypeScript files?

前端 未结 9 1554
北恋
北恋 2020-11-28 20:29

When using the TypeScript plugin for vs.net, how do I make one TypeScript file import modules declared in other TypeScript files?

file 1:

module moo
         


        
相关标签:
9条回答
  • 2020-11-28 20:41

    Since TypeScript 1.8+ you can use simple simple import statement like:

    import { ClassName } from '../relative/path/to/file';
    

    or the wildcard version:

    import * as YourName from 'global-or-relative';
    

    Read more: https://www.typescriptlang.org/docs/handbook/modules.html

    0 讨论(0)
  • 2020-11-28 20:41

    used a reference like "///<reference path="web.ts" /> and then in the VS2013 project properties for building "app.ts","Typescript Build"->"Combine javascript output into file:"(checked)->"app.js"

    0 讨论(0)
  • 2020-11-28 20:43

    If you're using AMD modules, the other answers won't work in TypeScript 1.0 (the newest at the time of writing.)

    You have different approaches available to you, depending upon how many things you wish to export from each .ts file.

    Multiple exports

    Foo.ts

    export class Foo {}
    export interface IFoo {}
    

    Bar.ts

    import fooModule = require("Foo");
    
    var foo1 = new fooModule.Foo();
    var foo2: fooModule.IFoo = {};
    

    Single export

    Foo.ts

    class Foo
    {}
    
    export = Foo;
    

    Bar.ts

    import Foo = require("Foo");
    
    var foo = new Foo();
    
    0 讨论(0)
  • 2020-11-28 20:44

    From TypeScript version 1.8 you can use simple import statements just like in ES6:

    import { ZipCodeValidator } from "./ZipCodeValidator";
    
    let myValidator = new ZipCodeValidator();
    

    https://www.typescriptlang.org/docs/handbook/modules.html

    Old answer: From TypeScript version 1.5 you can use tsconfig.json: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

    It completely eliminates the need of the comment style referencing.

    Older answer:

    You need to reference the file on the top of the current file.

    You can do this like this:

    /// <reference path="../typings/jquery.d.ts"/>
    /// <reference path="components/someclass.ts"/>
    
    class Foo { }
    

    etc.

    These paths are relative to the current file.

    Your example:

    /// <reference path="moo.ts"/>
    
    class bar extends moo.foo
    {
    }
    
    0 讨论(0)
  • 2020-11-28 20:52
    import {className} from 'filePath';
    

    remember also. The class you are importing , that must be exported in the .ts file.

    0 讨论(0)
  • 2020-11-28 20:53

    If you are looking to use modules and want it to compile to a single JavaScript file you can do the following:

    tsc -out _compiled/main.js Main.ts
    

    Main.ts

    ///<reference path='AnotherNamespace/ClassOne.ts'/>
    ///<reference path='AnotherNamespace/ClassTwo.ts'/>
    
    module MyNamespace
    {
        import ClassOne = AnotherNamespace.ClassOne;
        import ClassTwo = AnotherNamespace.ClassTwo;
    
        export class Main
        {
            private _classOne:ClassOne;
            private _classTwo:ClassTwo;
    
            constructor()
            {
                this._classOne = new ClassOne();
                this._classTwo = new ClassTwo();
            }
        }
    }
    

    ClassOne.ts

    ///<reference path='CommonComponent.ts'/>
    
    module AnotherNamespace
    {
        export class ClassOne
        {
            private _component:CommonComponent;
    
            constructor()
            {
                this._component = new CommonComponent();
            }
        }
    }
    

    CommonComponent.ts

    module AnotherNamespace
    {
        export class CommonComponent
        {
            constructor()
            {
            }
        }
    }
    

    You can read more here: http://www.codebelt.com/typescript/javascript-namespacing-with-typescript-internal-modules/

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