typescriptnamespacests-node.d.tsbabel-node

How to make ts-node aware of a namespace declared in a `d.ts` file?


I have a MY_FILE.ts file like that:

const someFunction = (param: MY_NAMESPACE.PARAM) : boolean => { // DO SOMETHING };

The param type is accessed through a namespace called MY_NAMESPACE that I declare on a MY_NAMESPACE.d.ts file. Like this:

src/MY_NAMESPACE.d.ts

declare namespace MY_NAMESPACE {
  type PARAM: SOME_TYPE
}

I need to run that MY_FILE.ts file, which contains a script.

This works fine:

npx babel-node src/MY_FILE.ts --extensions ".ts"

And this does not work (and I expected it to work just fine):

npx ts-node src/MY_FILE.ts

I get this error: error TS2503: Cannot find namespace MY_NAMESPACE

enter image description here

Note: In my real case, the MY_NAMESPACE is called TYPES.

How can I make it work with ts-node ?


Solution

  • I had a similar issue. The problem was, that ts-node is ignoring the include-option of the tsconfig.json and only following imports/refs in the starting file (see https://github.com/TypeStrong/ts-node#help-my-types-are-missing).

    I solved the issue by using the files option of ts-node in the tsconfig.json.

    {
        "compilerOptions": {...},
        "ts-node": {
            "files": true
        },
        "include": [
            "./src/**/*.ts",
            "./libs/**/*.d.ts"
        ]
    }
    

    There are other solutions for it (triple-slash directives, typeRoots). Just read the link above.