Transpilation does not occur for enums in external files

I have an Angular 1.x application built with Gulp. My enums are defined in a separate auto-generated file named enums.ts.

The enum content is as follows:

declare module MyApp.App {
export enum Status { 
    Done = 0, 
    Unsuccessful = 1, 
    Pending = 2,
}

The class that utilizes this enum is located in a different file called class.ts

namespace MyApp.App {    
  export class ResourcesCtrl implements IResourcesCtrl {
      public loading: boolean;
      public resources: IResource[];

      public isSucessfull(resource: IResource): boolean {
          return resource.status.toString() !== Status.Done.toString();
      }
  }

   angular.module("app").
      controller("resourcesCtrl",ResourcesCtrl);
}

When the file is processed by the TypeScript compiler, the output JavaScript file is always empty.

If I move the enum to the same file as the class that uses it, everything works correctly.

namespace MyApp.App {
  export enum Status { 
      Done = 0, 
      Unsuccessful = 1, 
      Pending = 2,
  }

  export class ResourcesCtrl implements IResourcesCtrl {
      public loading: boolean;
      public resources: IResource[];

      public isSucessfull(resource: IResource): boolean {
          return resource.status.toString() !== Status.Done.toString();
      }
  }

   angular.module("app").
      controller("resourcesCtrl",ResourcesCtrl);
}

Answer №1

This is not a piece of implementation code; it's actually an ambient declaration.

The usage of declare indicates that this section only provides information about types and does not contain actual implementation logic.

If you want to generate output from this file, you will need to include some implementation code within it.

As shown in the example below...

declare module DoesNotExist {
    export enum AmbientEnum {
        a, b, c
    }
}

export enum RealEnum {
    d, e, f
}

The resulting output file will only include RealEnum:

define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var RealEnum;
    (function (RealEnum) {
        RealEnum[RealEnum["d"] = 0] = "d";
        RealEnum[RealEnum["e"] = 1] = "e";
        RealEnum[RealEnum["f"] = 2] = "f";
    })(RealEnum = exports.RealEnum || (exports.RealEnum = {}));
});

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

When deciding between final char and enum for static data, which one would be more suitable?

Should I opt for enums instead of chars and ints in my Poker Java program? From what I gather, giving a separate integer value to a char makes it easier to use mathematical operators when comparing card values to determine a winner. However, I am not sure ...

Splitting code efficiently using TypeScript and Webpack

Exploring the potential of webpack's code splitting feature to create separate bundles for my TypeScript app has been a priority. After scouring the web for solutions, I stumbled upon a possible lead here: https://github.com/TypeStrong/ts-loader/blob/ ...

Can we stop Angular components from being destroyed during navigation?

Consider the scenario where I have two distinct Angular 2 components known as ComponentA and ComponentB. My goal is to navigate from ComponentA to ComponentB and then return to ComponentA without needing to reinitialize it. In the current setup of Angular ...

The shop named 'someStore' is currently unavailable! Please ensure that it is being offered by a valid Provider

I'm having trouble setting up a new project using React, Typescript, and MobX. Despite having a relatively simple piece of code, I can't seem to get MobX to work properly. It keeps showing me this error message: Uncaught Error: MobX injector: S ...

Exploring Typescript: Uncovering the Secrets of the navigator.connection Property

I am trying to access the NetworkInformation interface by using a simple TypeScript function like the one shown below: private checkNetworkConnection(): void { const connection = Navigator.connection || navigator.mozConnection || navigator.webkitConn ...

Error: In Angular Firebase, the type 'string' cannot be assigned to the type 'Date'

I am encountering an error. The following error is shown: "cannot read property 'toDate' of undefined. Without the toDate() | Date." After further investigation, I found: A Timestamp object with seconds=1545109200 and nanoseconds=0. A hel ...

The value of an Angular array seems to be disappearing after being copied to another array and then cleared

I have encountered an issue while working with arrays. I am initializing two arrays - one with some values and another as empty. However, when I assign the items from the first array to the second array and then clear the first array, it unexpectedly clear ...

Can dynamic string types be declared in Typescript?

Let's consider the following scenario: export enum EEnv { devint, qa1 }; export type TEnv = keyof typeof EEnv; export const env:Record<TEnv, {something:number}> = { devint: { something: 1, }, qa1: { something: 1, }, } Now, I ai ...

Determining whether a Typescript AST node represents a javascript native function

How can I determine if an AST node in TypeScript represents a valid JavaScript function, as opposed to a custom method? Here's what I'm thinking: function isJavascriptFunction(node: ts.Node): boolean { // ----- } For instance, given the cod ...

What is the proper way to combine two arrays containing objects together?

I am faced with the challenge of merging arrays and objects. Here is an example array I am working with: [ { name: "test", sub: { name: "asdf", sub: {} } }, { name: "models", sub: {} } ] ...

What steps are required to utilize NgbSortableHeader for sorting a bootstrap table through programming?

I have a bootstrap HTML table (operated by ng-bootstrap for Angular and utilized NgbdSortableHeader to arrange table columns by clicking on the column). When I click on an element, it sorts the column in ascending, descending, or ''(none) order. ...

What is the best way to assign default values when destructuring interfaces within interfaces in TypeScript?

My goal here is to create a function that can be used with or without arguments. If arguments are provided, it should work with those values; if not, default values should be used. The issue I'm facing is that although there are no TypeScript errors ...

JavaScript: How to clear an array after using a forEach loop

I'm currently developing a project for managing a store using NestJS and Mongoose. Within my code, I am trying to update certain items in the database and store these updated items in an array for later use. const updatedItems: Item[] = []; purchase ...

Typescript Error: TS2339: The property 'faillogout' is not found within the type '{ failed(): void; onSubmit(): void; }'

I encountered an issue with my Vue.js app using TypeScript. The error message I'm getting is: Property 'faillogout' does not exist on type '{ failed(): void; onSubmit(): void; }'. 101 | failed () { This snippet shows the s ...

Is there a way to retrieve a particular object from a versatile function?

Here is a generic function to consider: public getData<T>(url: string): Observable<T> { return this.httpClient.get<T>(url); } I am looking for a way to test this function by returning a mock object array, especially if the remote ...

The modal in Angular15 will automatically show the date decremented by 1 day whenever it is displayed

Every time I open the date field in a modal, it decrements by one day. Here is the associated typescript file: dob!: DatePipe; rescueDate!: string; dateAdded!: string; openEditPetMenu(template: TemplateRef<any>, petId: number, name: string, ...

Enhance your React Native experience with IntelliSense recommending react-native/types over react-native

I am trying to bring in <View from react-native, but instead, I am getting react-native/types https://i.sstatic.net/FeRKT.png How can I resolve this issue? This is a new project starting from scratch and I followed the documentation by adding TypeScri ...

Is it a good idea to combine both dynamic and static information within a react state?

As I ponder the data structure of my application, I've stumbled upon an intriguing question: Within my code, there exists some data that is predetermined and will remain constant throughout the lifespan of my app. Let's consider this data to be ...

Nextjs and Typescript are giving me an error that says, "The property 'nextUrl' is not found on type 'IncomingMessage'." What could be causing this

I'm currently utilizing the Next.js API route and attempting to retrieve the request parameters: export const GET = async (req: IncomingMessage) => { const matchString: String = req.nextUrl.searchParams.get("data") } I assume the typ ...

Tips for solving a deliberate circular dependency in an angular provider

If the existing injection token for this provider is available, I want to use it. Otherwise, I will use the specified provider. Below is the code snippet: providers: [ { provide: DesignerRecoveryComponentStore, useFacto ...