javascriptangulartypescriptangular8golden-layout

Typescript: Import namespace when namespace and class have the same name in a library file


I am having issue with accessing namespace while importing it from a file that declares the namespace and a class with the same name. I can access the class but not the namespace.

From the docs, I thought importing from a library that exports merged namespace and class will give you properties from both declarations. But, I am getting properties from the class only.

Namespaces are flexible enough to also merge with other types of declarations. To do so, the namespace declaration must follow the declaration it will merge with. The resulting declaration has properties of both declaration types. TypeScript uses this capability to model some of the patterns in JavaScript as well as other programming languages.

Here is my scenario,

Library file:

class GoldenLayout {
}

namespace GoldenLayout {
    export interface Config {
    }
}

In my project, I am trying to use Config interface. I am trying to use it this way,

import * as GoldenLayout from 'golden-layout';

const INITIAL_LAYOUT = GoldenLayout.Config = {
};

However, I get an error

Property 'Config' does not exist on type 'typeof GoldenLayout'.

I can access properties and method in the class GolderLayour, but I don't know how I can access the namespace.

For Reference, I am trying to use this library in my Angular 8 app.


Solution

  • I think your issue is that your not defining Config but rather double assigning. Try this:

    const INITIAL_LAYOUT: GoldenLayout.Config = { };