Encountering 'namespace undefined' error when using a namespace with ts-node

16 Views Asked by At

I create a helpers.ts file like this:

//helpers.ts
namespace helpers {
  export function genDescArr(from: number, to: number): number[] {
    let arr: number[] = [];
    for (let i = from; i >= to; i--) {
      arr.push(i);
    }
    return arr;
  }
}

and when using genDescArr function in other file like this:

//insert.ts
/// <reference path="./helpers.ts"/>
 const array = helpers.genDescArr(100, 1);

compile with ts-node and got error ReferenceError: helpers is not defined

I change helpers.ts to helpers.d.ts and use declare for helpers namespace but got same error!

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

package.json:

{
  "devDependencies": {
    "ts-node": "^10.9.2"
  },
  "dependencies": {
    "typescript": "^5.3.3"
  }
}
0

There are 0 best solutions below