Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An interface cannot be exported in an angular 2 module?

Tags:

I tried to export an interface in a NgModule-declaration and export and getting this error already in the editor (Visual Studio Code): [ts] 'MyInterface' only refers to a type, but is being used as a value here.

Here is the example code Edit-1:

import { NgModule }           from '@angular/core'; import { CommonModule }       from '@angular/common'; import { FormsModule }        from '@angular/forms'; import { MaterialModule }     from '@angular/material'; import { MyInterface }        from './my.interface'; import { MyService }          from './my.service';  @NgModule({   imports:      [ CommonModule, FormsModule,  MaterialModule.forRoot()  ],   declarations: [ MyInterface],//<- this is causing the message   exports:      [ MyInterface],   providers:    [ MyService ] }) export class MyModule { } 

One part of an explanation I found in the answer to this post: "since interfaces are erased at runtime in TypeScript". I'm currently refactoring my app to feature modules, so I cannot test it right now: Can I use the interfaces only by import from './mypathto/my.interface'?

like image 773
Myonara Avatar asked Feb 19 '17 20:02

Myonara


People also ask

Can we export interface in angular?

you can see bellow code for defining interface. export:The export keyword will help to use import this class on other component and class. interface:The interface ia a keyword so using this keyword you can set interface. Student: Student is a class name that describe interface details.

Can you export an interface in TypeScript?

Use a named export to export an interface in TypeScript, e.g. export interface Person{} . The exported interface can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

Which member has no exported version?

The error "Module has no exported member" occurs when we try to import a member that doesn't exist in the specified module. To solve the error, make sure the module exports the specific member and you haven't mistyped the name or mistaken named for default import.

What is export type 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.


1 Answers

You cannot export an interface. You can only export:

  1. Other modules
  2. Components
  3. Directives
  4. Pipes

NgModule is an angular concept and should not be confused with a typescript module. To make a third party, who uses your module, able to use your interface, you should create a .d.ts definition file with your module.

If you want to use a interface inside another NgModule of yours, you should just use:

import {InterfaceName} from 'path/to/ngmodule/to/interface'; 

Also, do not put an interface in the declarations array, this is only used for pipes/components/directives.

If you want your interface to be usable outside of an library, you should add it to the export of your index.ts:

export {InterfaceName} from 'path/to/ngmodule/to/interface'; 
like image 189
Poul Kruijt Avatar answered Sep 20 '22 05:09

Poul Kruijt