Zbigniew Zbigniew - 1 year ago 91
TypeScript Question

Typescript exporting and reexporting interface and other data

I am new to typescript. How can I export typedefinitions (classes, interfaces) + all variables from specific file? I tried this:


export interface CounterState {
count: number;

let t = 5;
export {t};


import * as models from './models';
models.t -> ok
models.CounterState -> not visible, why?
export default { models };


import CounterStore from "./index";

CounterStore.models.t -> ok
CounterStore.models.CounterState -> not visible, why?

Why * is not importing everything?
If I do it this way:
import {CounterState} from "./models";
it will work.

edit: If I change interface to class it works as expected.

Answer Source

The problem is not with import/export but with the fact that you are trying to evaluate an interface, which is not possible:

  • the actual interface declaration does not compile to any javascript, so trying to call models.CounterState has no meaning in the compiled js.

  • if you declare a class however, you do have a compiled result. This explains why this will not throw an error.

I just want to give another shout-out to the extreme obscurity of typescript errors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download