I’m a new user of Typescript and Deno and I don’t really understand how the interfaces scope are working.
I have a tree layer application :
The first layer (App.ts) is my application, this is where the core logic part of my application is. This part can be any file that need to refer to the second layer (API.ts).
The second layer (API.ts) is the entry point where I can communicate with any distant API (like Facebook, stripe, Google API)
The third layer is a bunch of .ts file. The second layer (API.ts) can communicate with each of them. Every file handles a very precise part of a particular API. I have for example one FB_page.ts to handle the part of the Facebook API responsible for pages, another file STRP_Subscription that handle the part of the Stripe API responsible for subscriptions, etc.
My problem is
I have a lot of interfaces to handle every type of parameter, every error and every response an API call can input or output. And I need these interfaces in every layer of my application.
It would be very cool if I can put interfaces of a particular API part inside the file that is responsible for it. For example, if I need several interfaces for the Facebook page API, I would like to put then inside the FB_page.ts file.
But it’s tedious to have to import the FB_page.ts file if I need them in the first layer of my application App.ts like this :
import {IPost, IPost_Error, IPost_Response} from '../lib/FB_page.ts';
So I put them inside .d.ts files, but they seem not to be global either ?
How can I have access to my interfaces without having to import them everywhere in my application ?
Thank you for your help.
2
Answers
Try exporting namespaces
Almost everything in Deno is a module. If you provide a non-module/script with global types, it won’t be found. Also, Deno cannot deal with
.d.ts
file extension, except for a few places with compiler hints.What you can do
.ts
.import
/export
)Note, that
deno
uses its own integrated TS compiler different from the one of your IDE (VS Code and others). So an IDE showing no type error doesn’t mean, thatdeno run
ordeno cache
can compile sources.Example code
If you want to separate the global types from your other code, you could use a side-effect import:
global.ts: