Does it make sense to use `export` within a `d.ts` file?

Viewed 38

I'm trying to create a file for the types that are globally used around my app.

reduxState.d.ts

declare namespace MyProject {
  type Type1 = someType;
  interface SomeInterface {
    someProperty: someType
  }
}

With the code above, I can already see my namespace and its members available across my project files.

So what is the difference between the code above, and the following code, that uses export for the namespace members?

declare namespace MyProject {
  export type Type1 = someType;
  export interface SomeInterface {
    someProperty: someType
  }
}

They both seem to work just fine. What is the difference?

From: https://www.typescriptlang.org/docs/handbook/namespaces.html#namespacing

Because we want the interfaces and classes here to be visible outside the namespace, we preface them with export.

In this excerpt from the DOC, it seems that they are referring to a namespace that is declared within a ts and not a d.ts file. Is that why you need the export in that case?

Does it make sense at all to use export within a d.ts file?

1 Answers

*.d.ts files are for definition, they get completely ignored at run-time. If you write JS code because you have a library or something, I'd heavily recommend using the export keyword, because this makes it way easier for other users to see what they will be able to use from your code (something you don't export, can't be imported elsewhere obviously).

Now your use-case: Everything that is exported from a namepsace, can be used outside of independently. Meaning you could do something like

const obj: MyProject.SomeInterface = { someProperty: 20 };

Since you're using *.d.ts files and not *.ts files, it won't make any difference for you as long as you only export interfaces, since they would be omitted at compile-time anyways.

One last note: The *.d.ts files make's it possible for you to see your stuff in your whole project but if you plan on importing functions or similiar from there, you need to import them first obviously.

Related