How can I export an interface that I have imported?

19,935

Solution 1

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;

Solution 2

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;

Solution 3

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

Solution 4

foo.ts

export interface ITest {
  ...
}

bar.ts

import * as foo from "./foo";

export type ITest = foo.ITest;
Share:
19,935
KFox
Author by

KFox

Updated on June 03, 2022

Comments

  • KFox
    KFox almost 2 years

    I am creating a library in typescript, which is spread across multiple files. I take all the classes and constants I have defines and import them into one module, which exports them all under one namespace. I have just defines an interface, and I wish to include it in the same namespace/module as all the other parts of my library. But apparently I can't.

    Here's a simplified example:

    /app.ts is the entry point of the application, all I do in it at the moment is include my library MyLib:

    //app.ts
    import myLib = require("lib/MyLib/MyLib"); // this works fine
    

    /lib/MyLib/MyLib.ts is the file in which I import all of the things defined by MyLib, and export them together:

    // lib/MyLib/MyLib.ts
    
    import VehiclesImport = require("./transport/vehicles");
    // error under  VehiclesImport.IAutomobile, saying that VehiclesImport has no property IAutomobile
    export var IAutomobile = VehiclesImport.IAutomobile; 
    export var Car = VehiclesImport.Car;
    

    In /lib/MyLib/transport/vehicles.ts, I define several classes and interfaces of vehicles, here, I'll just show IAutomobile and Car:

    // lib/MyLib/transport/vehicles.ts
    
    export interface IAutomobile {
        weight: number
    }
    
    export class Car implements IAutomobile {
        weight = 3000
    }
    

    I have tried creating a class truck in MyLib.ts, which properly implements IAutomobile, and that works fine, without any error messages. The problem only seems to arise when I want to access IAutomobile outside of an 'implements' statement.

    I apologize if this seems like a 'code dump', but in my opinion, this is a serious problem that I cannot access my interfaces except in a class declaration. I have searched Google for the past two hours and found nothing on the subject. Thanks for any help you can give me!

    Edit: I understand that typescript interfaces are not part of the compiled javascript code, but that should not stop me from manipulating them within typescript.

  • KFox
    KFox almost 10 years
    Very useful explanation and solution, thank you. I just have one question, will b.B be useable as an interface in c.ts?
  • KFox
    KFox almost 10 years
    Great explanation, and a helpful link, the only reason I hesitate to mark it as the accepted answer is that it lacks any workaround/solution, when other answers have provided one.
  • basarat
    basarat almost 10 years
    @KFox be useable as an interface in c.ts already done in foo:b.B. If you meant extendable updated answer to demonstrate that as well.
  • KFox
    KFox almost 10 years
    Yes that is what I meant, sorry for the confusion I wrote that fairly late at night. Thanks so much! I wasn't sure I'd be able to get around this problem.
  • user276641
    user276641 almost 8 years
    Thanks, updated the link to current version of the doc.
  • ffxsam
    ffxsam almost 6 years
    import a = require('./a') is invalid syntax.
  • Shaohua Huang
    Shaohua Huang almost 2 years
    will this implicitly convert interface into a type?