Search code examples
node.jstypescripttypescript2.2

d.ts file cannot "see" type declaration


I have this structure:

dts/
  my-types.d.ts
lib/
  a.ts
  a.d.ts
  b.ts
  b.d.ts

a.ts and b.ts both reference a certain type, lets's call it IFoo.

Because they share IFoo, I want to put that declaration in a shared location, so I put IFoo in dts/my-types.d.ts.

Looks like this:

interface IFoo {
  [key: string]: any
}

Makes sense right?

But the problem I have is that although IFoo is recognized in a.ts, b.ts, and c.ts, once my declaration files are created, in

a.d.ts
b.d.ts

IFoo can no longer be found in those files. For example, in one of the d.ts files I have:

declare var _default: (depList: string[], depContainerObj: IFoo) => Promise<any>;
export = _default;

and IFoo cannot be found. Why is that? How can I fix this?

Here is a clue!

When I change this:

interface IFoo {
  [key: string]: any
}

to this:

export interface IFoo {
  [key: string]: any
}

now the situation is reversed - my d.ts files can see the interface, but my .ts file cannot! What is going on?


Solution

  • What is going on?

    Inconsistent use of module. Please use modules wherever possible aka no globals. So do export interface IFoo {

    my d.ts files can see the interface, but my .ts file cannot! What is going on?

    Import the file containiing IFoo in each file that needs it e.g. a.ts:

    import {IFoo} from "./foo";
    // now use IFoo
    

    More

    A lot has been said about modules https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html