Encountering an error with TypeScript in combination with Angular 2 and Grunt. The error message is TS

Currently in my angular2 Project, I am utilizing grunt for automating the compilation of my typescript files.

Everything seems to be working fine as my files are compiling, but I keep encountering errors:

app/webapp/ng2/audit_logs/auditLogs.ts(2,3): error TS2304: Cannot find name 'app'.
app/webapp/ng2/audit_logs/auditLogs.ts(3,3): error TS2304: Cannot find name 'http'.
app/webapp/ng2/audit_logs/auditLogs.ts(3,10): error TS2304: Cannot find name 'ng'.
app/webapp/ng2/audit_logs/auditLogs.ts(6,3): error TS2304: Cannot find name 'app'.
app/webapp/ng2/audit_logs/auditLogs.ts(7,5): error TS2304: Cannot find name 'ng'.
app/webapp/ng2/main.ts(16,24): error TS2304: Cannot find name 'upgradeAdapter'.

I have attempted using Definetlytyped without success.

I'm at a loss as to why this is happening?

Please assist me in rectifying this. Below are my main.ts and auditLogs.ts files for you to review and pinpoint where the issue lies.

Main.ts file

/// <reference path="../../../node_modules/angular2/typings/browser.d.ts" />

var window: any = {
    giddh: undefined
};

import {UpgradeAdapter} from 'angular2/upgrade';

var adapter = new UpgradeAdapter();
var app = window.giddh.webApp;

adapter.bootstrap(document.body, ['giddhWebApp']);

// downgrade ng2 components to ng1 directives
app.directive('myApp', upgradeAdapter.downgradeNg2Component(app.AppComponent));

auditLogs.ts file

(function(giddh: any) {
  app = giddh.webApp
  http = ng.http

  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      templateUrl: ''
    }).Class({
      constructor: function(http: any) {
        this.test = new test(1020);
      }
    });

  class test {
    val: string;
    constructor(data: any){
      this.val = data;
    }
  }

})(window.giddh || (window.giddh = {}));

var window: any = {
  giddh: undefined
}
declare var require: any;
import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '/public/webapp/ng2/audit_logs/audit-logs.html'
})

export class AppComponent { 
}

Answer №1

There are several issues with the code snippet below:

(function(giddh: any) {
  app = giddh.webApp
  http = ng.http


  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      templateUrl: ''
    }).Class({
      constructor: function(http: any) {
        this.test = new test(1020);
      }
    });

  class test {
    val: string;
    constructor(data: any){
      this.val = data;
    }
  }

})(window.giddh || (window.giddh = {}));

var window: any = {
  giddh: undefined
}

Your file is a module, so you don't need an IIFE. Additionally, there is no need to use window. More information can be found here: https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Furthermore, you cannot use http globally. Angular must inject it for you.

Lastly, ng is only for type information and does not actually exist at runtime.

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

In case of an API error with the Nuxt fetch method, ensure that the entire site responds with a 404

Is it possible to configure a Nuxt website to respond with a 404 error when the API raises a 404 error? The API call is made on the client side using the fetch method: Check out this demo here: codesandbox demo Code Snippets index.vue <template> ...

Adjusting Spacing Between Characters

I've been looking for a way to convert regular characters (ABC) to full-width characters (ABC), but I haven't had any luck so far. That's why I'm turning to you guys for help. Currently, I don't have any JavaScript code writt ...

AJAX error encountered: TypeError - the properties 'arguments', 'callee', and 'caller' are inaccessible in the current context

While conducting an API call on a particular system, I encountered an error. Interestingly, I am able to obtain a response using curl and Postman with the same URL; however, Safari throws this error when employing Angular's $http.get() method. The is ...

Preventing Angular $rootElement.on('click') from affecting ReactJS anchor tag interactions

Running both AngularJS and ReactJS on the same page has caused an issue for me. Whenever I click on a ReactJS <a> tag, Angular's $rootElement.on('click) event is triggered and the page redirects. I need to perform some functionality in Re ...

The index.js file in ReactJs is failing to run completely

A couple of months back, I delved into the world of React but eventually put it on hold. Today, I decided to pick it back up and took the following steps: npm init npm install npm start Everything seemed to run smoothly (No Errors), but strangely nothing ...

Creating a Javascript Regular Expression to detect both accented and non-accented variations of a given string

Is there a way to use regular expressions in JavaScript to match both accented and non-accented versions of a string? I am customizing jQuery-ui's autocomplete feature by adding bold HTML tags around words in the suggestions. Here is my code snippet: ...

Error: The function $scope.apply is invalid and cannot be executed

I am attempting to display the contacts list after retrieving it using rdflib.js. The data is being loaded and stored in the list within the scope. However, I am running into an issue where the $scope is not updating, and it appears that I may be calling ...

Tips on integrating Cheerio with Angular framework

My Angular website is encountering errors in Google Dev Tools's console when I utilize a Cheerio method load('<h2 class="title">Hello world</h2>'); as per the instructions on the Github page. This application is fresh, and the s ...

Generate a type error if the string does not correspond to the key of the object

How can I trigger a type error in TypeScript 4.4.3 for the incorrect string 'c' below, which is not one of the keys of the object that is passed as the first parameter to the doSomething method? const doSomething = ({ a, b }: { a: number, b: stri ...

Leveraging the power of literal types to choose a different type of argument

My API is quite generic and I'm looking for a typed TypeScript client solution. Currently, my code looks like this: export type EntityTypes = | 'account' | 'organization' | 'group' export function getListByVa ...

AngularJS and the JavaScript programming language

Struggling with opening an Excel sheet by clicking on a button? I've written the code, but encountering issues. function openFile(strFilePath) { var objExcel; //Create EXCEL object objExcel = new ActiveXObject("Excel.Applicati ...

Removing error messages upon form reset initiated by an API request

Is there a way to clear error messages that appear beneath a text field when resetting form fields with values from an api call? In my Formik form, I have three fields that are loaded from an api database call along with a Reset button that reloads these ...

Leveraging the typeof Operator within a Class

How can we utilize typeof in order to specify the type of a class property? Take a look at both examples below, where example A works but example B does not. A) Works outside class const data: {age:number, name:string} = {age:10, name:'John'}; c ...

Unexpected Quote Will Not Appear

My random quote generator is not functioning properly, it should display a different quote on each click of the button. My colleagues are also facing the same issue. It was working fine when implemented in JavaScript, but after converting all the syntax to ...

What is the best way to retrieve the request object within a Mongoose pre hook?

Is there a way to have the merchantID in documents automatically set to the logged-in user found in req.user when saving them? product.model.js: const ProductSchema = new Schema({ merchantId: { type: ObjectId, ref: "Merchant", requ ...

Using Multiline Strings for Arguments

Is there a way to successfully pass multi-line strings containing spaces and tabs as a parameter to an express server? Below is the Express.js code snippet which accepts the parameter: app.get('/:prompt', async (req, res) => { ...

Swap out internal Wordpress hyperlinks for Next.js Link Component

Currently, I am working on a project where I'm using WordPress as a headless CMS with GraphQL for my Next.js app. Most aspects are running smoothly except for the internal content links within articles that are fetched through the WP API. These links ...

Creating a new JavaScript object using a Constructor function in Typescript/Angular

Struggling with instantiating an object from an external javascript library in Angular/Typescript development. The constructor function in the javascript library is... var amf = { some declarations etc } amf.Client = function(destination, endpoint, time ...

Error: The text value is not defined

Hello, I am currently working on retrieving the selected value of an input when the input is focused with the tab key. Below is my code: HTML <input id='texteb' type="text" value='' /> <button> click </button> JS ...

Improving the code of a JavaScript compiler through refactoring

I recently delved into the intricacies of a JavaScript package compiler and decided to revamp its fundamental structure. Each time a string is compiled, it gets appended to the SrcTable array and then outputted. However, for the output to be obtained, the ...