Typescript is failing to compile classes in a reliable sequential order

In my MVC 5 project, I am using TypeScript and angular. There are three TS files: Controller1.ts, Controller2.ts, and app.ts. The issue arises when I compile the program for the first time - everything usually compiles correctly in the expected order. However, upon making a change and triggering a recompile, the ordering of classes in the outputted app.js file changes. This results in the contents of app.ts (where the angular app is created) running before one or both controllers are defined, leading to this error:

[ng:areq] Argument 'ProviderScorecardController' is not a function, got undefined

Here is how my app.ts file is structured:

/// <reference path="_app.d.ts" />

module mqApp {
'use strict';

    if (typeof (angular) != "undefined") {

        var modules = [];

        angular.module("mqApp", modules)
            .controller("Controller1", Controller1)
            .controller("Controller2", Controller2);
    }
}

The structure of both controllers is similar, differing only in the class name:

/// <reference path="../../_app.d.ts" />

module mqApp {
    'use strict';

    export class Controller1 {
        public static $inject = [
            '$scope'
        ];

        private scope: angular.IScope;

        constructor($scope: angular.IScope) {
            this.scope = $scope;
            console.log("Master Controller Instantiated");
        }
    }
}

My _app.d.ts file, which is referenced from the above TS file, looks like this:

/// <reference path="_references.d.ts" /> [This just contains angular/jquery references]
/// <reference path="code/controllers/Controller2.ts" />
/// <reference path="code/controllers/Controller1.ts" />
/// <reference path="app.ts" />

Despite changing the order of the references, the issue persists. What could be causing this inconsistency in compiling the correct order?

The project utilizes Angular 1.6 and TypeScript 1.8.

Answer №1

It seems likely that the issue stems from the compiler attempting to establish a dependency tree among the files, resulting in a circular structure that may disrupt the expected order of compilation.

The fact that all your files are referencing the same _app.d.ts file implies a mutual interdependence among them, leading to potential complications.
Using a single d.ts file is generally recommended when incorporating external libraries (e.g., through typings) as it prevents any connection with your source files, thus eliminating the risk of circular dependencies.

In this scenario, you should consider:

// app.ts file

/// <reference path="./_references.d.ts" />
/// <reference path="./code/controllers/Controller1.ts" />
/// <reference path="./code/controllers/Controller2.ts" />

...

Additionally:

// Controller1.ts

/// <reference path="./_references.d.ts" />

...

You can create shared d.ts files for common functionalities, but be cautious not to form a circular reference loop.

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

What is the best way to set a checkbox to null instead of false using Angular?

I am currently developing a filtering system that allows users to select different parameters to filter through a list of items. For instance, the item list could be a collection of dishes with filters represented as checkboxes labeled as vegan, vegetaria ...

AngularJS filtering displays only the matched word as the result, rather than the entire string

Is there a way to only display the matched word in ng-repeat using filters? Let's say we have the following strings [ {text: 'This is first text from the array.'}, {text: 'Text in this array is dummy and it makes no sense'}, {te ...

Establish and define the global variable "Protractor"

In order to pass a string value from the function editPage.CreateEntity();, you can use that value in multiple test cases by assigning it to the variable entityName. You are able to retrieve the value by setting up the test case as follows: fit('Te ...

Explore Silverlights assortment using JavaScript

I have embedded a Silverlight class into my page, and in App.xaml.cs this component is registered to allow calls to Silverlight methods from JavaScript, which is functioning correctly. However, I now want to access not just methods, but collections. For e ...

Troubleshooting: Issue with WCF service not processing POST requests

I encountered an issue when attempting to call a WCF service using AJAX from another project. The error message displayed was: The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected me ...

Having issue updating a MySQL table using an array of objects in JavaScript

Working on a personal project involving React.js for the front-end, Node.js/express for the back-end, and mySQL for the database. The current array is as follows: horaires = [ { jour: 'Lundi', horaire: 'Fermé' }, { jour: 'Mar ...

Initiate a POST request to download the file upon clicking the button

How can I initiate a file download when a button is clicked? During testing, I noticed that sending a GET request using <Link href="/api/generate-pdf"> works perfectly and the PDF file gets saved. However, when I use a button to hit the API, the dow ...

What is the best approach to create a dynamic value from axios response in a reactive object?

I am attempting to retrieve data from the backend (specifically the user role) and store it in a reactive container with Vue: import {reactive} from "vue"; import axios from "axios"; export const store = reactive({ auth: axios.get ...

Implementing specific actions based on user selections in an Oracle APEX navigation bar

Within the application's navigation bar, there is a Home item that is present on all pages. I want to implement a feature where if the user clicks on the Home item, a warning message will pop up based on the page number, alerting them that any unsaved ...

Angular data binding with an object instead of an array list

Currently, I am implementing Angular and attempting to iterate through an object. Data in JSON format employee {"fName":"mike","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ebb3b3b3b3b3b3b3b3ab83849f868a8287c588848 ...

Ways to mimic an Angular directive with a required field

Recently, I encountered a challenge that involves two directives: directive-a, directive-b. The issue arises because directive-b has a `require: '^directive-a' field, which complicates unit testing. In my unit tests, I used to compile the direc ...

Tips for inserting a string into an array nested within an object stored in a state array

Currently, the variable sizeVariant is a string array and I am trying to append strings to it using an onClick event. The function findIndex seems to be working fine. However, there seems to be an issue with the concatenation section. It appears that using ...

Is the accuracy of the in-situ convolution filter guaranteed?

After analyzing my simple box blur function, I have come to the realization that it may be incorrect. The current implementation edits the ImageData object in place, leading to convolution filters depending on surrounding pixels that have already been ch ...

Unable to execute findOneAndUpdate as a function

I've been researching all morning and testing different solutions, but I'm still unable to resolve this issue. Every time I run the code below, I receive the error "TypeError: req.user.findOneAndUpdate is not a function": req.user.findOneAndUpd ...

Is there a way to establish a connection between two excel entries using Angular?

In order to connect xlsx file records with their corresponding ids using angular, I am seeking a solution. To elaborate further: Let me provide an example for better understanding: Scenario 1 https://i.stack.imgur.com/25Uns.png Scenario 2 https://i ...

Obtaining Master-Detail table data via WebAPI

I have a scenario where I am working with a database that consists of two tables linked by a foreign key relationship, following the classic master-detail structure. My goal is to retrieve all the data from these tables using ASP.NET MVC WebAPI calls from ...

Tips for setting up a personalized preview mode in Sanity Studio using Next.js

I am facing an issue displaying the preview mode because the URL must contain specific parameters such as "category" and "slug" (as shown in the image below). Here is the error URL with undefined parameters Therefore, I am unable to retrieve the paramete ...

Resetting a form in React JS: A step-by-step guide

How can I clear the input values in a React JS form upon clicking Reset? class AddFriendForm extends Component { constructor(props) { super(props) this.state = { firstname: '', lastname: '', } } render() { c ...

Command for Sniping with Discord.js

I am currently working on creating a snipe command using Discord.js in my bot. I have set up command handlers and everything seems to be working fine, including the on messageDelete event. However, I encounter an error when I delete a user message and try ...

What is the correct method for importing React in TypeScript (.tsx) to ensure optimal integration?

Within our TSX/React web application, we employ two distinct import styles for the react module: import * as React from "react"; as well as import React from "react" As far as I know, both methods function without any noticeable differences. Is there a ...