skip to Main Content

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


  1. Try exporting namespaces

    export as namespace ICalendar;
    import { Document } from "mongoose";
    
    export interface Calendar extends Document {
    userId?: Object
    }
    
    Login or Signup to reply.
  2. 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

    1. Rename the files containing the interfaces to .ts.
    2. Put global types in a module (file with import/export)
    3. Make sure to import this module, so Deno CLI can find it when compiling

    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, that deno run or deno cache can compile sources.

    Example code

    // Put following code inside one of your used modules
    declare global {
      interface IPost {
        foo: string;
      }
    }
    
    // IPost will be available globally without import
    const post: IPost = { foo: "bar" }
    

    If you want to separate the global types from your other code, you could use a side-effect import:

    import "./lib/global.ts";
    const post: IPost = { foo: "bar" };
    

    global.ts:

    export {}; // use some export to mark file as module
    declare global {
      interface IPost {
        foo: string
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search