Home > Enterprise >  How to use Triple-Slash Directives in ts-node
How to use Triple-Slash Directives in ts-node

Time:06-27

I am just studying about typescript and its module system.

Meanwhile, I found out Triple-Slash Deirectives.

I just thought it is alternative expression of import / export. but It don't work as a thought. In below codes, I want to use namespace Myconsole in test.ts files.


// Myconsole.ts

namespace MyConsole {
  export function log(msg: string) {
    console.log(msg);
  }
}



// test.ts
/// <reference path="Myconsole.ts" />



MyConsole.log("log"); // ReferenceError occurs here. but no redline

namespace MyConsole2 { 
  export function foo(msg: string) {
    console.log(msg);
  }
}

MyConsole2.foo("ging"); // but this one works.  

but when I ran ts-node test.ts it spit out Reference Error: Myconsole is not defined

What makes me more confused is complie with tsc -d command is working.

// test.js

/// <reference path="Myconsole.ts" /> // Do .ts file available in .js ...?
var MyConsole2;
(function (MyConsole2) {
    function foo(msg) {
        console.log(msg);
    }
    MyConsole2.foo = foo;
})(MyConsole2 || (MyConsole2 = {}));
MyConsole.log("log"); // ReferenceError occurs here. but no redline
MyConsole2.foo("ging");

I am so confused... How can I properly use Triple-Slash Directives?

Do I set additional config?? please give me an advise

CodePudding user response:

Triple-slash reference files, from the documentation, are used as Typescript compiler directives. Triple-slash references instruct the compiler to include additional files in the compilation process. But the files are not imported, just the type declarations are.

Reference files are for type declarations and type declarations only, not the definitions. They make the compiler happy because the type declarations would exist but as soon as node try to run it fails with ReferenceError as it doesn't have any of the definitions to point to.

You can see it fails with tsc too the same way ts-node does. Try it:

First compile the file with tsc test.ts. The output test.js (the compiled javascript output) is going to be something like this:

/// <reference path="Myconsole.ts" />
MyConsole.log("log"); // ReferenceError occurs here. but no redline
var MyConsole2;
(function (MyConsole2) {
    function foo(msg) {
        console.log(msg);
    }
    MyConsole2.foo = foo;
})(MyConsole2 || (MyConsole2 = {}));
MyConsole2.foo("ging"); // but this one works.

And as soon as you try to run it with node test.js you see the same error:

> node test.js
/test.js:2
MyConsole.log("log"); // ReferenceError occurs here. but no redline
^

ReferenceError: MyConsole is not defined
    at Object.<anonymous> (/test.js:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    ...

See, the compiled test.js have the same Triple-slash reference but node itself doesn't recognize it as it's Typescript specific. It only recognises import/require to include definitions. And when it runs MyConsole is not defined anywhere in the scope.

  • Related