KFox
KFox

Reputation: 1266

How can I export an interface that I have imported?

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.

Upvotes: 19

Views: 29119

Answers (6)

Liran H
Liran H

Reputation: 10579

In TypeScript 3.9.6, this worked for me:

import { Foo as FooType } from './some-path';

export type Foo = FooType;

Upvotes: 0

Ghost in the Shell
Ghost in the Shell

Reputation: 73

This works to re-export types/interfaces

import type { MyInterface, MyType } from './randomModule';

export { MyInterface, MyType }

The key is the surrounding braces in the export statement. Works in TypeScript 4.7.4. Reference.

Upvotes: 6

Oleg
Oleg

Reputation: 506

foo.ts

export interface ITest {
  ...
}

bar.ts

import * as foo from "./foo";

export type ITest = foo.ITest;

Upvotes: 3

attdona
attdona

Reputation: 18943

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;

Upvotes: 14

Douglas
Douglas

Reputation: 37761

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

Upvotes: 7

basarat
basarat

Reputation: 275867

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;

Upvotes: 14

Related Questions