1

I'm using typescript and not able to use the declared interfaces from another file.
My global.d.ts looks like this.

declare interface IPropSendEmail {
    from: string,
    to: string,
    subject: string,
    html: string,
}

below is tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "./build",
        "esModuleInterop": true,
        "declaration": true,
        "strict": true,
        "typeRoots": [
            "types"
        ],
    }
}

the directory structure looks like this

    | -- src
            |-- types
                     |-- global.d.ts
            |-- util
                     |-- common.ts
            |-- ...

using it in common.ts file

export async function sendEmail({ from, to, subject, html }: IPropSendEmail) {
     // function code here
     ...
}
mkamranhamid
  • 583
  • 4
  • 18

1 Answers1

2

It should work. However, I suggest to clean the weird part of your configuration:

In your tsconfig.json:

  • Remove typeRoots;
  • Add include.
{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "build",
        "esModuleInterop": true,
        "declaration": true,
        "strict": true
    },
    "include": ["src"]
}

In global.d.ts, the keyword declare is useless:

interface IPropSendEmail {
    from: string,
    to: string,
    subject: string,
    html: string,
}

Ensure there is no export or import in this file.

Paleo
  • 21,831
  • 4
  • 65
  • 76
  • Thanks for the reply and suggestions for tsconfig.json. I have used `declare` so that I can use that `interface` in any file without importing it. I've used it previously it's just I don't have that configuration now. – mkamranhamid Oct 23 '20 at 08:13
  • @mkamranhamid Just try to remove it (the `declare` keyword). It'll work the same way. – Paleo Oct 23 '20 at 08:21
  • I think you're referring [this](https://stackoverflow.com/questions/42233987/how-to-configure-custom-global-interfaces-d-ts-files-for-typescript) by @Andris. This is a very detailed answer on why using `declare` is a bad practice. – mkamranhamid Oct 23 '20 at 08:34
  • @mkamranhamid No, I'm really saying that the keyword `declare` is useless with an interface. – Paleo Oct 23 '20 at 09:07
  • then? are you saying I should use `export` so that the `interface` will be available to `import` in `common.ts` – mkamranhamid Oct 23 '20 at 09:40
  • @mkamranhamid I suggest you read the text on the lines and stop reading between the lines. Seriously! https://stackoverflow.com/a/49156180/3786294 – Paleo Oct 23 '20 at 10:56
  • I did that thing but It didn't work. To make it work I have to remove all imports statements from `common.ts` as suggested by [@Andris](https://stackoverflow.com/questions/42233987/how-to-configure-custom-global-interfaces-d-ts-files-for-typescript) – mkamranhamid Oct 23 '20 at 11:24
  • @mkamranhamid It works. I just tried. And nobody suggested to remove all imports from `common.ts`. – Paleo Oct 23 '20 at 13:40