Zbigniew Zbigniew - 3 months ago 11
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:

models.ts

export interface CounterState {
count: number;
}

let t = 5;
export {t};


index.ts

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


reducer.ts

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

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.

Comments