0

Can I export a namespace A with another namespace B in it? Something like:

// b.ts
export namespace B {
  export const val = 'val';
}
// a.ts
export namespace A {
  //... some thing import b as namespace 
}

--- above will be export as a module and depended by another project C;

// c.ts
import { B } from 'A';

const a = B.val;

And I hope ts show me 'namespace B' in C instead of 'import B', which seems to be impossible 😢;

another question is: if I can split namespace B into multi files when I export, like:

// b2.ts
export namespace B {
  export const val2 = 'val2';
}

and it can be imported in C

// c.ts
import { B } from 'A';

const b2 = B.val2;

1 Answer 1

3

I had the same problem, and finally came across this answer on the Typescript Github repo.

The work around is as follows:

// b.ts (no change here)
export namespace B {
  export const val = 'val';
}

in a.ts

// a.ts
import {B as _b} from './b.ts'
export namespace A {
  export import B = _b
}

And finally in C.ts

import { A } from './a.ts'
const b2 = A.B.val;

As for the second part of your question, i think this and this may help you out.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.