Tips on exporting a basic TypeScript class in an Angular 4 module

I am facing a challenge with packaging a TypeScript class as part of an Angular module for exporting it as a library using ng-packagr.

For instance, here is my class definition -

export class Params {
    language: string ;
    country: string ;
    variant:  string ;
    dateFormat:  string ;
    briefDateTimeFormat:  string ;
    decimalFormat:  string ;
}

This is how I have defined my module -

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Params } from '../params/params';
import { LoginService } from '../sso/login.service';

@NgModule({
  imports: [CommonModule],
  exports: [Params],
  providers: [LoginService]
  declarations: [Params, LoginService]
})
export class FoundationModule { }

However, when trying to export the FoundationModule in public-api.ts of ng-packagr, I encounter the following error -

BUILD ERROR
: Unexpected value 'Params in .../params/params.ts' declared by the module 'FoundationModule' in '.../foundation.module.ts'. Please add a @Pipe/@Directive/@Component annotation.

I am seeking guidance on how to successfully package a single class within an Angular module.

Answer №1

Consider the following two choices:

export class Params {
language: string;
country: string;
variant: string;
dateFormat: string;
briefDateTimeFormat: string;
decimalFormat: string;
constructor(obj: any) {
    this.language = obj && obj.language || null;
    this.country = obj && obj.country || null;
    this.variant = obj && obj.variant || null;
    this.dateFormat = obj && obj.dateFormat || null;
    this.briefDateTimeFormat = obj && obj.briefDateTimeFormat || null;
    this.decimalFormat = obj && obj.decimalFormat || null;
}

}

or

export interface Params {
    language: string;
    country: string;
    variant: string;
    dateFormat: string;
    briefDateTimeFormat: string;
    decimalFormat: string;
}

It is important to note that, as mentioned in the previous response, classes or interfaces should not be exported within the module.

Reference on Stackoverflow

Further explanation in Angular documentation

Answer №2

The issue lies within your module definition. It is necessary to import Params directly into every component or service that necessitates the class.

public-api.ts

import { Params } from '../params/params';

...
newVariable: Params = {
  language: 'my language',
  ...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a NextJS client component within a webpage

I am currently working with NextJS version 14 and I am in the process of creating a landing page. In one of the sections, I need to utilize the useState hook. I have specified my component as "use-client" but I am still encountering an error stating that " ...

What is the best way to set up TypeScript to utilize multiple node_modules directories in conjunction with the Webpack DLL plugin?

Utilizing Webpack's DllPlugin and DllReferencePlugin, I create a distinct "vendor" bundle that houses all of my main dependencies which remain relatively static. The project directory is structured as follows: project App (code and components) ...

The issue I'm facing with Angular 8 is that while the this.router.navigate() method successfully changes the URL

As someone who is relatively new to Angular, I am currently in the process of setting up the front end of a project using Angular 8. The ultimate goal is to utilize REST API's to display data. At this point, I have developed 2 custom components. Logi ...

Arrange the columns in Angular Material Table in various directions

Is there a way to sort all columns in an Angular material table by descending order, while keeping the active column sorted in ascending order? I have been trying to achieve this using the code below: @ViewChild(MatSort) sort: MatSort; <table matSort ...

Is it necessary for @Input() to come before @ViewChild in Angular components? And what is the reason

Within my ImageUploaderComponent, I have defined an @Input variable and an @ViewChild element. export class ImageUploaderComponent implements OnInit { @Input() canvasheight: number; @ViewChild('cropper', undefined) ... } Interestingly, th ...

Issues arise with file compilation in Angular 5

I want to apologize in advance for my English. I am encountering a problem when running `ng build --prod` on my Angular project. The following errors are being returned: ERROR in Error: Error: Internal error: unknown identifier undefined at Object. ...

Display a nested component post initialization in Angular

<ng-container *ngIf="isTrue; else notTrue"> <app-child [property1]="value" [property2]="value" [property3]="value" (function1)="func($event)" ></app-child> </ng-container> <ng-t ...

Deployment error on Google App Engine for Angular Universal 13 and Angularfire 7

Upgrading my app from Angular 12 to Angular 13 has caused deployment issues on Google App Engine. The error message reads: ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/ ...

Creating a Button with Icon and Text in TypeScript: A step-by-step guide

I attempted to create a button with both text and an icon. Initially, I tried doing it in HTML. <button> <img src="img/favicon.png" alt="Image" width="30px" height="30px" > Button Text ...

Classify the array types based on their indexes within the map

Is there a way to efficiently access elements in TypeScript using a map with the correct type? When attempting this, an error is thrown due to the type from the map being number | string type Tuple = [number, number, string]; const y: Tuple = [1, 2, &apos ...

"Exploring the world of npm packages alongside the powerful angular-cli tool

Is it better to package angular2 components in an npm module with the source files (*.ts, *.css, *.html) or the webpack compiled version for use in applications compiled with angular-cli@webpack? What should actually be published in the npm package? The r ...

Is it possible to determine the type of an array value similar to how we can determine the type

Here is something I can accomplish: const keys = { "hi": {name: "ho"} } type U = [keyof typeof keys][0]; // "hi" But can I also achieve the same with array values? const data = [ { name: "hi" } ]; type T = typeof data[0]["name"]; // string not " ...

Obtaining undefined values for req and resolvedUrl in GetServerSideProps function

In my project, I am currently using next.js version ""next": "^12.1.4"" and node version ""@types/node": "^14.14.6". I have created a function called getServerSideProps with parameters req and resolvedUrl. When the ...

Error in custom TypeScript: Incorrect error instance detected within the component

I encountered a unique issue with my custom Error export class CustomError extends Error{ constructor(message: string) { super(message); Object.setPrototypeOf(this, CustomError.prototype); this.name = "CustomError"; } Furthermore ...

Unable to initialize a public variable due to issues with Ionic Storage retrieval

I am currently facing an issue where I am trying to assign a token stored in the Ionic storage module to a public variable. However, when I attempt to set the token and then access it from another function, I encounter an undefined error. Here is the code ...

Discover the secret to extracting a unique style from inspect mode and incorporating it into your CSS design

I'm currently working on my angular project and I've imported the ngx-editor. My goal is to reduce the height of the editor, but I'm facing some challenges. After inspecting the element, I was able to adjust the height successfully within th ...

Encountering a promise error when using TypeScript and React Lazy

I've been working with React and TypeScript, incorporating a higher order component to verify user authentication. However, after implementing the hoc, I encountered an error in my routes: /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/rem ...

"Exploring the process of extracting data from a JSON URL using NextJS, TypeScript, and presenting the

After tirelessly searching for a solution, I found myself unable to reach a concrete conclusion. I was able to import { useRouter } from next/router and successfully parse a local JSON file, but my goal is to parse a JSON object from a URL. One example of ...

Using React with Typescript and ie18next to fetch translations from an external API

In the past, I have experience working with i18next to load translations from static json files. However, for my current project, I need to load all translations from an API. How can I achieve this? Additionally, how can I implement changing the translat ...

Ways to keep information hidden from users until they actively search for it

Currently, I have a custom filter search box that is functioning correctly. However, I want to modify it so that the data is hidden from the user until they perform a search. Can you provide any suggestions on how to achieve this? Below is the code I am u ...