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'?
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.
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.
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.
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.
You cannot export an interface. You can only export:
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With