question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the purpose of using `declare` and `export` together?

See original GitHub issue

src/Components/Footer/Footer.d.ts

declare namespace IFooter {
    export interface IProps {}
}

export { IFooter };

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
crisfcodescommented, Apr 21, 2020

@futuredayv

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well). https://www.typescriptlang.org/docs/handbook/modules.html

then according to the above, all you need is to remove all import and export from the file and automatically the declarations within the file will be available in the global scope.

src/Components/Footer/Footer.d.ts

declare namespace IFooter {
  interface IProps {}
}

src/Components/Footer/index.tsx

import React from 'react';

const Footer: React.FunctionComponent<IFooter.IProps> = (): JSX.Element => (
  <div className="footer">Footer</div>
);

export { Footer };

As you note here, you can still use IFooter.Iprops without having to import it into the file. This pollute the Global namespace.

4reactions
crisfcodescommented, Apr 22, 2020

@futuredayv

pros

The TypeScript code we write is in the global scope by default. If we have multiple files in a project, the variables, functions, etc. written in one file are accessible in all the other files. So its easier to use types defined in other files.

cons

Your are polluting the global namespace, anybody can easily override variables declared in the global scope without even knowing they are doing so. This is a dangerous space as it can lead to conflicts/erros in the code.

“Magically available interfaces” or global types is highly discouraged and should mostly be left to legacy. Also, you should not be using ambient declaration files (e.g. d.ts files) for code that you are writing. These are meant to stand-in the place of external non-typescript code (essentially filling in the typescript types into js code so that you can better integrate it with javascript). For code you write you should be using plain .ts files to define your interfaces and types. https://stackoverflow.com/questions/42233987/how-to-configure-custom-global-interfaces-d-ts-files-for-typescript

Most large/mature TypeScript libraries avoid relying on ambient declarations and instead export anything that needs to be reused outside of the current file. For example, the Angular codebase has an interfaces.ts file for each module which is imported into each file that needs to reference the interfaces: https://stackoverflow.com/questions/48690533/typescript-declare-vs-export-best-practices


The thing here is why use export and declare together? There is a good reason to use this pattern?

some libraries like next/router use it:

export declare function useRouter(): NextRouter;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Modules - TypeScript
Importing an exported declaration is done through using one of the import forms ... module into a single variable, and use it to...
Read more >
export - JavaScript - MDN Web Docs
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the ......
Read more >
Export and Import - The Modern JavaScript Tutorial
We can label any declaration as exported by placing export before it, be it a variable, function or a class. For instance, here...
Read more >
What is export default in JavaScript ? - GeeksforGeeks
Default Exports: Default exports are useful to export only a single object, function, variable. During the import, we can use any name to...
Read more >
Understanding C++ Modules: Part 2: export, import, visible ...
(The only weird one is the export keyword in the module-declaration, which is just a re-use of the keyword and does not actually...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found