How can I export an interface that I have imported?
Use the import
keyword to bring in something into the type declaration space (as opposed to var
which brings it into the variable declaration space).
This is demonstrated below. a.ts
:
export interface A {
val: number;
}
To re-export this from another file b.ts
:
import a = require('./a');
export import B = a.A; // Important use of import
Sample usage in some other file c.ts
:
import b = require('./b');
var foo: b.B;
foo.val = 123;
interface C extends b.B {
val2:number;
}
var bar: C;
bar.val2 = 456;
Types can't be assigned to variables, they exist in different "declaration spaces". Classes can be assigned to variables, because they contribute their names to the type declaration space as well as defining the class objects. Interfaces only contribute to the types declaration space, so can't be referenced as values.
The language is a bit verbose, but this is spelt out in detail in section 2.3 of the language spec
foo.ts
export interface ITest {
...
}
bar.ts
import * as foo from "./foo";
export type ITest = foo.ITest;
The example rewritten following TS language specification:
a.ts
:
export interface A {
val: number;
}
To re-export this from another file b.ts
:
export {A} from './a'
Usage in some other file c.ts
:
import {A} from './b'
var foo: A = {val: 2};
foo.val = 123;
interface C extends A {
val2:number;
}
var bar: C = {val: 1, val2: 3};
bar.val2 = 456;