Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 954
KFox Avatar asked Jul 28 '14 16:07

KFox


People also ask

Can we export interface in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

How do I export a imported function in TypeScript?

Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.

What is module export?

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.


2 Answers

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;
like image 69
basarat Avatar answered Oct 04 '22 05:10

basarat


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;
like image 33
attdona Avatar answered Oct 04 '22 05:10

attdona