Error in Angular 10/ Typescript: Cannot find property in type 'Object'.ts

Transitioning from Angular 1 to Angular 10 has led me to dive into learning typescript. Please bear with me if there are any errors as I navigate this new territory. While I have a substantial amount of code, here is the main issue at hand. In Angular 1, I was accustomed to using dot notation in my API responses. However, when attempting to do the same in Angular 10, an error emerges: Property 'CreditApplication' does not exist on type 'Object'.ts.

Despite encountering this error during the build process and within VS code, the code executes successfully and the data is correctly displayed in my HTML view without any console errors in the browser. The HTML view effectively accesses result.CreditApplication.PrimaryCustomer, even though the mentioned error persists.

            this.api.getApplication(id).subscribe(result =>{
            if(result){
                this.primaryCustomer = result.CreditApplication.PrimaryCustomer;
                this.financeOptions = result.CreditApplication.FinanceOptions;
            }
          })

To resolve this issue, I have crafted an interface where I import PrimaryCustomer and assign primaryCustomer accordingly:

export interface PrimaryCustomer {
 SSN: string;
 Id: number;
 CustomerReferences?: Array<any>;
 FirstName: string;
 LastName: string;
 Locale?: string;
 OptOutStatus?: boolean;
 IncomeSource: any;

}

In the component file, I declare:

primaryCustomer: PrimaryCustomer;

Here's a snippet from the working HTML view:

<strong>Bank Name:</strong> <span *ngIf="primaryCustomer && primaryCustomer.BankAccount">{{primaryCustomer.BankAccount.BankName}}</span>

I've explored solutions for similar errors on Stack Overflow, but haven't found an exact match for my particular case. It seems like I may need to typecast the result, but I'm uncertain. Any assistance would be greatly appreciated.

Answer №1

After some trial and error, I found the solution to my issue by creating interfaces for each variable in my code - result, primaryCustomer, and financeOptions. This required me to adjust my API call line to incorporate the interface for result:

this.api.getApplication(id).subscribe((result : Result)  =>{

Once all the interfaces were correctly set up, I was able to resolve all build errors. It was a valuable learning experience, and I appreciate the help from those who provided comments.

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

Convert the generic primitive type to a string

Hello, I am trying to create a function that can determine the primitive type of an array. However, I am facing an issue and haven't been able to find a solution that fits my problem. Below is the function I have written: export function isGenericType ...

Error in TypeScript due to object being undefined

Exploring TypeScript and facing a challenge with setting properties in an Angular component. When I attempt to define properties on an object, I encounter an error message: ERROR TypeError: Cannot set property 'ooyalaId' of undefined Here is ho ...

Encountering difficulties resolving the dependency tree for an angular-ionic project

Node version: v16.3.0 ng version: 12.1.0 Currently working on an Angular-Ionic 5 project Encountered an error while running the npm install commandhttps://i.sstatic.net/nYOJc.png 2. Also tried running npm install --force but faced a different error h ...

Can you provide guidance on how to divide a series of dates and times into an array based

Given a startDate and an endDate, I am looking to split them into an array of time slots indicated by the duration provided. This is not a numerical pagination, but rather dividing a time range. In my TypeScript code: startDate: Date; endDate: Date; time ...

What are some techniques for breaking down or streamlining typescript code structures?

Within my TypeScript class, I have a skip function. In the interface, I've specified that the data is coming from the backend. Now, on the frontend, I want to be able to rename the backend variables as demonstrated below. There are multiple variables ...

Encountered an error while trying to install @angular/cli globally using npm. The postinstall script for version [email protected] was unsuccessful

As a first time Angular installer on Windows 7, I am encountering an issue with the following versions: node -v is v8.10.0 and npm -v is 5.7.1 When trying to install Angular CLI with the command: npm install -g @angular/cli An error is occurring during ...

Issue with Datatables not loading on page reload within an Angular 7 application

Incorporating jQuery.dataTables into my Angular 7 project was a success. I installed all the necessary node modules, configured them accordingly, and added the required files to the angular.json file. Everything functioned perfectly after the initial launc ...

Angular Observable being triggered repeatedly

In my service, I have implemented a global observable in the following way: @Injectable() export class XYZService { country: Observable<any>; isBrowser: boolean; constructor(private http: Http, private httpClient: Htt ...

How can I change the date format from yyyymmdd to yyyy/mm/dd using an Angular pipe in HTML?

Can Angular convert a date in the yyyymmdd format to yyyy/mm/dd directly? I attempted using an Angular pipe, but it produced a different date result. The date I have is 20201029 {{date | date:'yyyy-MM-dd'}} ...

Display the next page once two distinct service requests have been received

When I need to display a page after retrieving data from two different services, service1 and service2, how can I achieve this without nesting the second service call inside the first one? Instead of chaining the service calls, I want to make separate req ...

Typescript useReducer hook not functioning properly when trying to access the object

In my types.ts file, there is a code snippet that looks like this: import { CategoryType } from "../../types/CategoryItemCount"; import { ModelErrorType } from "../../types/ModelError"; export const ACTIONS = { SET_CATEGORIES: " ...

Strangely behaving data binding when using socket.io

Recently, I've been experiencing some strange behavior while developing a socket.io app with Angular 2 on the frontend. This unusual issue seems to be related to the interaction between Angular 2 and socket.io, as I have never encountered anything lik ...

What is the process for utilizing the TypeScript compiler with nodejs?

I have a sample code saved in a file called hello.ts Upon the completion of nodejs setup on Windows, execute the following command to install typescript: npm install -g typescript Is there a way to compile hello.ts directly with node.js? While using "T ...

Acquiring the appropriate type from a type object using generics in TypeScript

I am working with an enum export const trackingKeys = { Form: 'form', Video: 'video', } as const I also have a type that assigns a type property to each key export type TrackingPropertiesByKey = { [trackingKeys.Form]: { bar : num ...

Angular 5 - capturing form inputs - activating event upon selecting suggested values

When I click on suggested values below the input field, the (click)="doSomething()" event doesn't fire. How do I handle this issue? I would like to be able to type something in the input field and then have an event triggered when clicking on the su ...

What is the best way to switch the CSS class of a single element with a click in Angular 2

When I receive data from an API, I am showcasing specific items for female and male age groups on a webpage using the code snippet below: <ng-container *ngFor="let event of day.availableEvents"> {{ event.name }} <br> <n ...

There seems to be a problem as I am unable to match any routes. Does anyone have any suggestions on how I can resolve this issue?

I am currently developing an Angular recipe application. I have a feature where users can click on the "Full Recipe" button in the saved recipes page to view detailed recipe information. The functionality works perfectly in the recipe item component, but w ...

What are the steps to transpile NextJS to es5?

Is it possible to create a nextjs app using es5? I specifically require the exported static javascript to be in es5 to accommodate a device that only supports that version. I attempted using a babel polyfill, but after running es-check on the _app file, ...

The names of properties in Typescript are determined by the values of the outer type properties

In my project, I have various interfaces (or types) defined as follows: export type simpleValue = string | number | boolean | Date | null; export interface Options { inline?: OptionsItem[] | unknown[]; promptField?: string; selectedValues?: unknown[ ...

Using Typescript generics to enhance arrays

I am looking to extend a generic list of Array that has been previously extended from my class. How can I accomplish this in the correct way? export interface DeliveryMethod { readonly id: string; readonly company: string; readonly cost: number; re ...