Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting an imported interface in TypeScript

Tags:

typescript

I have a number of templates in different directories - I define an interface for each so I can be sure that what I am referencing in my TypeScript code will be available in the template.

I would like to define an interface for each of them and then collate all of the interfaces in a single file that can be imported (so I can always just import that file and auto complete will show what interfaces are available).

However, I'm having trouble doing this. What I currently have:

login/interface:

export interface Index {
    error: string;
    value: string;
}

interfaces.ts:

import * as login from './login/interface';
export let View = {
    Login: login
};

login.ts:

import * as I from './interface';
...
let viewOutput: I.View.Login.Index = {
   ...
};

Results in:

error TS2694: Namespace '"...interface"' has no exported member 'View'.

However, if I try to access I.View.Login.Index as a simple variable then I get:

Property 'Index' does not exist on type 'typeof "...interface"'.

Which seems correct since the interface is not really a type, but it has been able to access I.View.Login to get that far.

I don't understand what I'm doing wrong or missing here I'm afraid. Any help would be greatly appreciated!

like image 909
Allan Jardine Avatar asked Jan 16 '17 20:01

Allan Jardine


People also ask

How do I import an interface in typescript?

TypeScript uses the concept of modules , in the same way that JavaScript does. In order to be able to import an interface from a different file, it has to be exported using a named or default export. The example above uses named exports and named imports.

What is the use of export in typescript?

As now we already know that export keyword is used to export the classes, interface, functions, and type in TypeScript and made them available to reuse the component by importing them inside any other module by using the import statement at the beginning of the TypeScript program.

How do I import an interface exported from another file?

The exported interfaces can be imported by using a named import as import {A, B} from './another-file'. You can have as many named exports as necessary in a single file. Here is an example of exporting multiple interfaces from a file called another-file.ts.

What is the difference between named and default exports and imports?

The main difference between named and default exports and imports is - you can have multiple named exports per file, but you can only have a single default export. If you try to use multiple default exports (for functions, classes, variables) in a single file, you would get an error.


1 Answers

You can re-export like so:

// interfaces.ts
export * from './login/interface';
export * from './something/interface';

And then:

// login.ts
import * as I from './interface';
let viewOutput: I.Index = { ... }

Another option is:

import * as login from './login/interface';
export { login as Login };

And then:

// login.ts
let viewOutput: I.Login.Index = { ... }
like image 72
Nitzan Tomer Avatar answered Oct 05 '22 03:10

Nitzan Tomer