1
votes

I'm unable to import a class that was exported two times.

a.ts

import * as moduleB from "./b";
export class A {
  b: moduleB.B;
  constructor() {
    this.b = new moduleB.B();
    this.b.hello();
  }
}

b.ts

import {C} from "./c";
export const B = C;

c.ts

export class C {
  hello() {
    console.log("hello");
  }
}

The error message is:

a.ts(3,14): error TS2305: Module '"b"' has no exported member 'B'.

The problem seems to be that the type of C has not been probably exported with "export const B = C;". The error would go away if I change "b: moduleB.B;" to just "b: any;". How can I fix this?

Using a default export in b.ts would work but I want to export several things in b.ts so that's not an option. I'm using Typescript 1.7.5.

1

1 Answers

1
votes

When you're declaring property b : moduleB.B in a.ts you're defining b to be of the type moduleB.B, but you've defined moduleB.B as a const. Since the const is a reference to the constructor function of C the new moduleB.B() does compile.

You could write export type B = C in b.ts instead, but that would then only export the type alias which would mean b : moduleB.B would compile, but new moduleB.B() wouldn't since you did not export constructor function.

Use export {C as B} (full alias) in b.ts to achieve exactly what you want to achieve.