TULC
TULC

Reputation: 45

Can an imported merged namespace be exported in Typescript?

I have a library with a number of merged declarations like:

export class Foo {...}
export namespace Foo {
   export class Bar {...}
   ...
}
export default Foo

The merged namespace typically defines inner classes, errors specific to the class, etc.

I would like to re-export some of these declarations in my own library. I am currently trying to do so with:

import FooExt from './library.js'

export namespace Baz {
   export type Foo = FooExt
   export const Foo = FooExt
   ...
}

This successfully exports the values of the Foo namespace, but not its types; i.e.:

import {Baz} from './mylib.js'

let x: Baz.Foo.Bar              // Error: Namespace '".../mylib".Baz' has no exported member 'Foo'.
x = new Baz.Foo.Bar             // Ok
if (x instanceof Baz.Foo.Bar) { // Ok
    // do something
}

Is there a way to export an imported merged namespace and preserve its types?

So far as I can tell, this is a different case to this question and this question.

Upvotes: 0

Views: 54

Answers (1)

TULC
TULC

Reputation: 45

The namespace can be exported using the export import syntax:

import FooExt from './library.js'

export namespace Baz {
   export import Foo = FooExt  // <-- exports namespace including types and values
   ...
   // instead of:
   //export type Foo = FooExt
   //export const Foo = FooExt
}

See the original answer in the TypeScript GitHub project.

Upvotes: 1

Related Questions