How to export the interface that I imported?

I am creating a library in typescript that spreads across multiple files. I take all the classes and constants that I define, and import them into one module, which exports them all under one namespace. I just defined an interface, and I want to include it in the same namespace / module as all other parts of my library. But apparently I can’t.

Here is a simplified example:

/app.ts is the entry point of the application, everything I'm doing at the moment includes my MyLib library:

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

/lib/MyLib/MyLib.ts is a file in which I import all 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 just show IAutomobile and Car:

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

I tried to create a cool truck in MyLib.ts that implements IAutomobile correctly, and it works fine, without error messages. The problem only occurs when I want to access IAutomobile outside the "tools" statement.

I apologize if this looks like a "dump code", but in my opinion, this is a serious problem that I cannot access my interfaces except in the class declaration. I have been searching Google for the past two hours and have not found anything about this. Thanks for any help you can give me!

Change I understand that typescript interfaces are not part of the compiled javascript code, but this should not stop me from manipulating them in typescript.

+7
source share
4 answers

Use the import keyword to import something into the type declaration space (as opposed to var , which brings it to the variable declaration space).

This is shown below. a.ts :

 export interface A { val: number; } 

To re-export it from another b.ts file:

 import a = require('./a'); export import B = aA; // Important use of import 

Example usage in another c.ts file:

 import b = require('./b'); var foo: bB; foo.val = 123; interface C extends bB { val2:number; } var bar: C; bar.val2 = 456; 
+9
source

Example rewritten after TS language specification :

a.ts :

 export interface A { val: number; } 

To re-export it from another b.ts file:

 export {A} from './a' 

Using in another c.ts file:

 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; 
+4
source

Types cannot be assigned to variables; they exist in different "ad spaces". Classes can be assigned to variables as they contribute their names to the type declaration space and also define class objects. Interfaces contribute to the type declaration space, so they cannot be referenced as values.

The language is a little detailed, but it is described in detail in section 2.3 of the language specification

+2
source

foo.ts

 export interface ITest { ... } 

bar.ts

 import * as foo from "./foo"; export type ITest = foo.ITest; 
0
source

Source: https://habr.com/ru/post/972971/


All Articles