1

Typescript defines comment with xml tag <reference path=""/> to source local files to current file. But that tag could be placed only in file header before declaring any structures such us other modules.

So,

// File1.ts - correct
///<reference path="./Common.ts"/> 
module Test {
    export class TestClass {
    }
}

// File2.ts - incorrect
module Test {
    ///<reference path="./Common.ts"/> // <<< Here is an compile error
    export class TestClass {
    }
}

Is it possible to source content of other typescript file to custom place of current file?

durron597
  • 31,968
  • 17
  • 99
  • 158
Pavel Patrin
  • 1,630
  • 1
  • 19
  • 33
  • This reference works like 'classpath' to let compiler know where to look for referenced types. It's have no connection to code structure. – setec Mar 06 '14 at 08:57
  • But tsc compiler includes file contents and puts it to place of `reference` tag (i tried to compile with --out option). – Pavel Patrin Mar 06 '14 at 11:29
  • Is interesting because i've never seen such a behaviour. When i compile ts it makes js keeping /// – setec Mar 06 '14 at 12:48

2 Answers2

1

In the latest version of TypeScript, it is not necessary to reference files manually like that. You can simply remove the reference and TypeScript will be able to figure everything out automatically.

Alex Dresko
  • 5,179
  • 3
  • 37
  • 57
1

No. You cannot require code inplace as you have already found:

// File2.ts - incorrect
module Test {
    ///<reference path="./Common.ts"/> // <<< Here is an compile error
    export class TestClass {
    }
}

However you can effectively get the same effect by using functions.

// File1.ts - correct
///<reference path="./Common.ts"/> 
module Test {

    callAFunctionFoundInCommon();

    export class TestClass {
    }
}
basarat
  • 261,912
  • 58
  • 460
  • 511