The error code TS2474 (TS) indicates that in 'const' enum declarations, the member initializer must be a constant expression

Error code:

export const enum JSDocTagName {
    Description = "desc",
    Identifier = "id",
    Definition = "meaning",
}

Implementing Angular 6 in conjunction with the .NET framework.

Answer №1

When transpiling constant enums, they are completely removed from the runtime application with no residual code left behind. Any instance of a constant enum is simply replaced by its value throughout the entire app. As a result, using the declare keyword with constant enums is unnecessary:

export const enum JSDocTagName {
    Desc = "desc",
    Id = "id",
    Meaning = "meaning"
}

Unlike regular enums, constant enums cannot have certain types of complex calculated values. For example, while normal enums allow expressions like the following, constant enums do not:

const x = 1;

enum A {
    Name = x,
    Age = x + 1
}

If you encounter an error related to this scenario, it may be due to attempting something similar within a constant enum.

However, you can still include some basic calculations in a constant enum as long as the outcome is deterministic. This means expressions like the following are valid for constant enums:

const enum A {
    Name = 1 << 0,
    Age = 1 << 1,
    Date = 1 << 2
}

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 theming feature in Angular 5 with Bootstrap 4 and Bootswatch seems to be malfunctioning

Having trouble implementing bootswatch themes with angular 5 and bootstrap 4? I've added the following to styles.scss: @import "~bootswatch/dist/cerulean/variables"; @import "~bootstrap/scss/bootstrap"; @import "~bootswatch/dist/cerulean/ ...

Prevent unnecessary requests for asset images in Angular 5

Within my Angular application (running version 5.1.0, built with angular-cli and webpack), I have a country selector component that allows users to choose a country from a drop-down menu or by typing the name in an autocomplete field. Each matching result ...

Retrieving the text value of a selected option with a reactive form

This is my select option HTML <select _ngcontent-c3="" formcontrolname="role" value="" ng-reflect-name="role"> <option _ngcontent-c3="" value="addrole" ng-reflect-value="addrole">--Add Role--</option> <option _ngcontent-c3="" v ...

Upon clicking a button, I aim to retrieve the data from the rows that the user has checked. I am currently unsure of how to iterate through the rows in my mat-table

My goal is to iterate through the column of my mat-table to identify the rows that have been checked, and then store the data of those rows in an array. <td mat-cell *matCellDef="let row"> <mat-checkbox (click)="$event.stopPropagation()" (c ...

Preventing Event Propagation in Angular HTML

I am encountering an issue with stopPropagation, and I need assistance with implementing it in HTML and TypeScript for Angular. The problem is that the dialog opens but also triggers a propagation. Below is my code snippet in HTML: <label for="tab-two ...

What is the best way to transform a JSON object into a personalized C# object?

Is there a simple method to populate my C# object with the JSON data received through AJAX? The following is an example of a JSON object passed to a C# web method using JSON.stringify: { "user": { "name": "asdf", ...

What is the definition of this ""!sequence"?

As part of my current project, I am diving into an outdated code library within my organization to document it and repurpose it for future projects. Surprisingly, there is absolutely no documentation in the Library, not even comments, but I am determined t ...

Ways to indicate in Typescript that a value, if it exists, is not undefined

Is there a way to represent the following logic in TypeScript? type LanguageName = "javascript" | "typescript" | "java" | "csharp" type LanguageToWasmMap = { [key in LanguageName]: Exclude<LanguageName, key> ...

Unable to utilize the namespace 'RouteComponentProps' as a specified type

When using TypeScript to define an interface that extends RouteComponentProp, I encountered some issues: VSCode error: [ts] Cannot use namespace "RouteComponentProps" as a type. Console error: Cannot use namespace 'RouteComponentProps' as a typ ...

Verifying whether the filter query will produce any matches

I'm currently working with a *ngIf statement and I need to show a specific message when the condition is not met. However, I'm facing difficulties in finding a way to achieve the opposite of the filtered result. Essentially, I want to display a m ...

Running NG BUILD from Gulp can be done using either child-process.spawn or by disabling all output in child-process.exec

Have you come across a similar question like this Call "ng build" from inside a gulp task? I have found a way to successfully build Angular using Gulp in order to prevent overflowing the output buffer. Here's how I achieved it: const child ...

What exactly does Type refer to in Angular 2?

I keep encountering the Type keyword in various parts of the documentation. For instance, in this link, it mentions that the ComponentRef has a property called componentType which is of type Type<any>. Upon further investigation, I stumbled upon this ...

I seem to have misplaced my memory. It appears that there is a substantial count of

I am currently facing a challenge with my WPF application that involves displaying a large number of images, both big and small. The issue at hand is the excessive memory usage by the app, and I am struggling to identify the source of this problem. When p ...

The Angular APP_INITIALIZER provider is encountering an issue where one injected service is being passed as undefined, while the rest are

One of my injected services is returning as undefined in my provider, despite both services running before the provider. Any help or insight would be greatly appreciated. Below is a snippet of my code from AppModule: @NgModule({ declarations: [ // ...

Exploring deep nested components and elements in Angular for a targeted specific functionality

Is it possible to apply the ng deep css class to only one specific checkbox in my component, rather than all checkboxes? I want to customize just one checkbox and leave the others unchanged. How can this be achieved? Thank you. I need the CSS modificatio ...

Encountering an error in Angular 2: "date.getMonth is not a function

I am currently utilizing the Angular-2-datepicker in my project. Everything seems to be functioning properly, but whenever I attempt to set the [(date)] attribute, an error is thrown. An error stating that date.getMonth is not a function keeps popping u ...

Tips on avoiding updates to a defined object when a new object is filtered (created from the original object)

Is there a way to filter an array of objects based on their year without altering the original object? Whenever I apply a filter, it affects both the newly created object and the original one. However, I need the original object to remain unchanged so that ...

Eliminate the underscore from mat-select in (@angular/material 15.0.3)

Is there a way to remove the underline from mat-select? <mat-form-field style="margin: 2em 2em 2em 2em" appearance="fill" > <mat-label>Choose an option</mat-label> <mat-select> <mat-option value=& ...

What is the best way to eliminate a specific set of characters from a string using TypeScript?

Imagine you have the following lines of code stored in a string variable: let images='<img alt="image1" height="200" src="image1.jpg" width="800"> <img alt="image2" src="image2.jpg" height="501" width="1233"> <img alt="im ...

One method of extracting an object from an array using a parameter function is by utilizing the following approach

I am searching for a specific object in an array based on the user-provided ID. var laptops = [{ "name": "Firefox", "age": 30, "id": "ab" }, { "name": "Google", "age": 35, "id": "cd", "date": "00.02.1990" }, { "na ...