manidos
manidos

Reputation: 3464

How to use an already declared type under a new namespace?

I'd like to use an already declared type under a new namespace. The code below gives me an error declaration or statement expected. What am I missing?

interface MyInterface {
  myProp: string;
}

declare namespace MyNamespace {
  export MyInterface; // this line errors
}

I found a solution, but it doesn't feel like the right one. What do you think?

interface MyInterface {
  myProp: string;
}

declare namespace MyNamespace {
  export interface MyInterface extends MyInterface {}
}

The reason I'm doing this is that I have components in my app and I want them to export types under a single namespace MyNamespace, so that I could use the type anywhere in the app without having to import them individually.

P.S The code above doesn't work as expected. The exported types are not visible globally. So, I ended up doing this (which is not a solution at all)

./translator/types.d.ts

declare namespace AppTypes {
  export interface Dictionary {
    [key: string]: {
      [key: string]: string
    }
  }
}

./config-service/types.d.ts

declare namespace AppTypes {
  export interface Config {
    [key: string]: boolean;
  }
}

anywhere in the app

const dictionary: AppTypes.Dictionary = {...}
const config: AppTypes.Config = {...}

Upvotes: 1

Views: 179

Answers (1)

Atul Bansode
Atul Bansode

Reputation: 229

You can use it by this way

 declare namespace MyNamespace {
    export interface MyInterface {
        myProp: string;
    }
  };

Upvotes: 3

Related Questions