Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom TypeScript Global Interfaces

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.

like image 235
wayofthefuture Avatar asked Jul 22 '15 01:07

wayofthefuture


People also ask

What is global D TS in TypeScript?

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: $(() => {

Can you import interfaces in TypeScript?

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.

Where do I put my interfaces TypeScript?

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.


1 Answers

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

like image 146
thoughtrepo Avatar answered Sep 21 '22 16:09

thoughtrepo