Ian Liu Rodrigues
Ian Liu Rodrigues

Reputation: 642

Declaration file for nested modules

I have a project with a Node JS module inside foo/bar.js. Now I'm writing a TypeScript module in src/mymod.ts which should import foo/bar.js. How do I write a declarations file for foo/bar.js module? Where should the declarations file go?

I've tried placing a declarations file in foo/bar.d.ts but when I import * as bar from "../foo/bar" in my TypeScript module, I get the following error:

File '/tmp/my_proj/foo/bar.d.ts' is not a module.

This is my tsconfig.json file

{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "./dist/",
    "target": "es2017",
    "module": "commonjs",
    "strictNullChecks": true
  },
  "include": [
    "./src/"
  ]
}

This is my declaration file:

// Type definitions for bar
declare module "bar" {
  function fun1(n: number) : number;
  function fun2(n: number) : number;
  function fun3(n: number) : number;
}

Upvotes: 0

Views: 1592

Answers (1)

Ian Liu Rodrigues
Ian Liu Rodrigues

Reputation: 642

The problem is fixed if I change the declarations file to the following:

export function fun1(n: number) : number;
export function fun2(n: number) : number;
export function fun3(n: number) : number;

I've came up with this solution when I've read @Gerrit0's comment. If someone can write a better answer with explanations I will change the answer.

Upvotes: 1

Related Questions