Is it possible to define a class prior to exporting it in typescript/angular? | TSLint warning: unused expression, assignment or function call expected

Coming from the realm of React, I am well-versed in the fundamental concepts of launching an application with an index.js, avoiding direct involvement with HTML, and utilizing import and export statements to share views among different JavaScript files. In React, export default designates the default import object, while export allows for importing multiple objects from a single file.

Given this background, how different is Angular really?

In an attempt to familiarize myself with Angular, I decided to start with a simple task of creating a logging class. It seemed straightforward, at least in the React context. Here's how I would have approached it:

import {Injectable} from '@angular/core';

@Injectable()
class LoggerService {
  info(msg: any) {
    console.log(msg);
  }

  warn(msg: any) {
    console.warn(msg);
  }

  error(msg: any) {
    console.error(msg);
  }
}

export LoggerService;

However, upon trying this, I encountered a warning from TSLint:

TSLint: unused expression, expected an assignment or function call...

Is it not allowed to define classes before exporting them in Angular?

I've noticed that most tutorials suggest keeping everything within the export class, which is different from what I'm accustomed to. If adhering to this practice is the Angular way, then I'll follow it.

TLDR: Why am I receiving an error with the code above?

Answer №1

When it comes to TypeScript versus JavaScript, the key difference lies in the syntax rather than the framework itself. If you're familiar with working with React using JavaScript modules, transitioning to TypeScript may require some adjustment. In TypeScript, the syntax may vary slightly. To achieve your desired outcome in TypeScript...

class LoggerService {
 // ...
}
export { LoggerService };

For further information on TypeScript modules, you can refer to TypeScript's modules documentation.

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

The IE browser is showing a blank page when I try to open my Angular 8 application

Even after incorporating all the necessary polyfills and making updates to the browserlist file and tsconfig file, I am still unable to identify a solution. Any assistance would be greatly appreciated. ...

Typescript error: The value "X" cannot be assigned to this type, as the properties of "Y" are not compatible

Disclaimer: I am relatively new to Angular2 and typescript, so please bear with me for any errors. The Challenge: My current task involves subtracting a start date/time from an end date/time, using the result in a formula for my calculation displayed as " ...

Set up the package generated from a custom build of a separate angular project

I am in the process of migrating all my projects from Angular 5 to Angular 6. Some of my projects have dependencies on others. For example, I have a project named ProjectA that depends on ProjectB. After building ProjectB, I end up with a set of files in ...

When working with Typescript, an error may occur related to index types even though the constant object and its

I am struggling with understanding TypeScript, specifically when it comes to a problem I encountered. Hopefully, someone can shed some light on this for me. My issue revolves around a functional component that is responsible for displaying the correct com ...

Is there a way to verify if a form has unsaved modifications prior to closing ng-modal in Angular 5?

Within my modal popup, I have a registration form that resembles a form-group. The form consists of fields such as text, checkbox, and select. Should a user make changes to any of the form controls without saving, then attempt to close the modal popup, I w ...

`How to cleverly fake dependencies with symbols in Jest, Vue3, and Typescript?`

I am faced with the following scenario: // symbols.ts - Injection Key defined as a Symbol export const FAQ_SERVICE: InjectionKey<FAQService> = Symbol('FAQService'); // main.ts - globally provides a service using the injection key app.provi ...

In order to conceal the div tag once the animation concludes, I seek to implement React

I'm currently using the framer-motion library to add animation effects to my web page. I have a specific requirement where I want to hide a div tag used for animations once the animation is complete. Currently, after the animation finishes, the div t ...

Inquiry regarding the return value of 'async-lock' in nodejs

I am utilizing the async-lock module in my typescript project to handle concurrency. However, I am encountering difficulties with returning the result within lock.acquire(...) {...}. Any guidance on how to resolve this issue would be greatly appreciated. ...

Using a function to implement *ngFor results in generating a loop

When using *ngFor in Angular with a function that returns data, the function gets called multiple times and can sometimes result in a loop: app.component.ts export class AppComponent { getArray(): string[] { //I can track when this function is c ...

Angular unit testing using Jasmin Karma encountered an error stating: "Unable to locate the name 'google'."

codeSnippet.js:- getAutocompleteResults(inputElement: ElementRef) { const autocomplete = new google.maps.places.Autocomplete(inputElement.nativeElement, { ... }); google.maps.event.addListener(autocomplete, 'place_changed&a ...

Tips for updating TypeScript aliases during compilation

To define a typescript alias in tsconfig.json, you can use the following syntax: "paths": { "@net/*":["net/*"], "@auth/*":["auth/*"], }, After defining the alias, you can then use it in you ...

Error in Typescript: The identifier 'Proxy' is unknown

I'm trying to create a new variable using the Proxy type from the ES6 specification: myProxy: Proxy; However, I'm encountering the following error: Cannot find name 'Proxy'. Can anyone point me in the right direction to resolve th ...

How can we determine programmatically if the dark theme has been applied in Ionic?

Currently, I am working on integrating Mapbox with Ionic angular. My main query is regarding how to determine which theme is applied in order to load the corresponding Mapbox theme. An example of what I am looking for: const currentIonicTheme = ionic.them ...

What is the best way to transfer an object from the view to the controller in AngularJS and ASP.net MVC

How to pass an object with a specific amount of data from the View to the Controller using ASP.net MVC and AngularJS VIEW var Person = {}; Person.IdPerson = 69425; Person.Year = new Date().getFullYear(); $http.post('/API/Update_Person', { para ...

Learn how to showcase vertical text in React Native similar to a drawer

Looking for a way to display text vertically in a dynamic manner, where the length of the text can vary. Below are some examples for reference: Example 1 Example 2 <View> <View style={{}}> <View style={{ marginTop: 30, flexDire ...

Leverage the power of TypeScript with knockout's pureComputed function

I am facing an issue with referencing the this object in a function called problem: const c = { f() { console.log("hi"); }, problem: ko.pureComputed(() => { return this.f(); }), }; [ts] The containing arrow function captures the glob ...

The RC-dock library's 'DockLayout' is not compatible with JSX components. The instance type 'DockLayout' is not a valid JSX element and cannot be used as such

Despite encountering similar questions, none of the provided answers seem to address the issue within my codebase. My project utilizes React 17, Mui v5, and TS v4. I attempted to integrate a basic component from an external package called rc-dock. I simply ...

Can you specify a data type for ngFor in Angular?

I am currently employing ngFor to iterate over a collection of a specific type [Menu] within Angular 4.x. During this process, I am looping through a collection property of the menu object (menu.items). Unfortunately, my IDE (Eclipse + Angular IDE) is un ...

Error: Unable to dispatch a Redux-thunk action with additional arguments in React

Currently, I am attempting to work with TypeScript alongside Redux-Thunk and Redux. My goal is to pass an object to any action (I am utilizing dependency injection and need to pass either an object service or a string). However, I encountered the followin ...

Troubleshooting the lack of success in enhancing global scope within Typescript

Currently, I am working on a microservices application where I have two very similar services that use practically the same packages. To perform some testing, I decided to add a function to the global scope and modified it slightly to prevent any TypeScrip ...