I have several interfaces that I use across an entire CommonJS structured typescript project. I would like to create a global file called "interfaces.ts" ... Rather than having to import the interface or add a reference into every .ts file (boilerplate), is there a way to globally declare it, maybe in the tsconfig.json file?
I am aware there is a global definition file called lib.d.ts, which is basically globally declared interfaces. I realize I can probably modify this file, but was looking for a better way (abstraction).
Edit: I should probably note that I'm currently using Visual Studio Code.
A global library is one that can be accessed from the global scope (i.e. without using any form of import ). Many libraries simply expose one or more global variables for use. For example, if you were using jQuery, the $ variable can be used by simply referring to it: $(() => {
TypeScript uses the concept of modules, in the same way that JavaScript does. In order to be able to import an interface from a different file, it has to be exported using a named or default export.
You can store interfaces directly on the main file that use them. Like any other class, function or object, you can explicitly export and import types from . ts files.
That's the way tsconfig.json
works by default. Create a tsconfig.json
in the root of your project without a "files" property. You may need to restart VS Code for it to recognize the tsconfig.json
.
If no "files" property is present in a tsconfig.json, the compiler defaults to including all files the containing directory and subdirectories. When a "files" property is specified, only those files are included.
https://github.com/Microsoft/TypeScript/pull/1692
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With