Exploring the concept of object inheritance in Angular 5 with Typescript

I'm facing a challenge related to inheritance while building my initial angular 5 application. The error message I encounter is: Property 'message' does not exist on type 'CouponEvent', as reported by the angular-cli.

export class Event {
public _eventId: number;
public _type: string;
public _name: string;
public _sendDate: Date;
public _message: string;

constructor(){}

public get message(): string {
 return this._message;
}

public get type(): string {
 return this._type;
}

public get name(): string {
 return this._name;
}

public get sendDate(): Date {
 return this._sendDate;
}

public get eventId():number {
 return this._eventId;
}
}

import './Event';
export class CouponEvent extends Event {
 _expirationDate: Date;

get expirationDate(): Date {
 return this._expirationDate;
}
}

While working with my util class, I encountered an issue trying to format the message and event.message was not found.

import { Injectable } from '@angular/core';
import {Event} from '../models/Event';
import {CouponEvent} from '../models/CouponEvent';

@Injectable()
export class UtilService {

formatMessage(event: CouponEvent): string {
  let msg = event.message.replace(/\${code}/gi,event.code);

  const date = event.expirationDate.toString
   msg = event.message.replace(/\${expiration}/gi,date);

  return msg;
 }
}

I would greatly appreciate any assistance in resolving this matter. As this is my first experience with TypeScript, I am somewhat confused about why it is not functioning correctly.

Thank you for your suggestions!

Answer №1

Perhaps this occurs due to the fact that Event is a built-in type in Typescript, which may result in naming conflicts. Consider changing the name of your class and/or using a different import statement like

import { Event } from './CustomEvent';

Answer №2

It finally clicked for me that consolidating all the classes into one file was the solution. I had previously placed each class in a separate file, but once I grouped them together, everything fell into place.

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 for me to generate a fresh object?

In one of my components, I have implemented a feature where clicking on an image toggles a boolean variable to show or hide a menu. The HTML structure for this functionality is as follows: <img src="../../assets/image/dropdown.png" class="dropdown-imag ...

Issue with displaying data on a Modal in Angular when clicking for the first time, but successfully displays on the second click

In my display screen, customer details are shown in a table format. When a row is clicked, a modal should pop up displaying detailed information about the customer. The issue I'm facing is that data is not populated on the first click, but it works fi ...

Easily implement a wide variety of fonts in your web projects by dynamically loading hundreds of font

I am in possession of a directory called /assets/fonts containing a plethora of fonts that I wish to incorporate into a website in a dynamic manner. Users will be able to specify their font preferences, akin to a text editor. Individually assigning the fo ...

Is there a forEach loop supported in Angular2? If so, why does it display all objects with the same value?

Hello everyone, I'm currently facing an issue with getting server response objects and passing them into a new Array object. In the code snippet below, you can see that when I try to print these newly passed objects using a forEach loop, they appear a ...

utilize the useRef hook to display the total number of characters within a text area

Introducing the following component: import React from 'react'; export interface TexareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { maxLength?: number; id: string; } export const Textarea = React.forwardRef( ( ...

After upgrading Expo, the React Native Auth Session ceased to function

During my use of Expo SDK 48, my app successfully implemented Google and Facebook authentication with a web browser-based authentication method. Functional code: type AuthResponse = AuthSession.AuthSessionResult & { params: { access_token ...

Error: Import statement cannot be used outside a module (@cucumber/cucumber) while using Node.JS, Playwright, and Cucumber framework

I encountered an issue while attempting to compile my Node.js code that is compliant with ECMAScript 6: $ npx cucumber-js --require features/step_definitions/steps.ts --exit import { Before, Given, When, Then } from "@cucumber/cucumber"; ^^^^^^ ...

Tips for avoiding the reconstruction of components in if-else conditions within Angular

After just joining the Angular4 club as a ReactJS developer, I find myself using basic conditional statements with a very simple condition. Take a look: <div class="app-component"> <app-country *ngIf="condition"></app-country> ...

Angular's responsiveness is enhanced by CSS Grid technology

I am attempting to integrate a CSS grid template that should function in the following manner: Columns with equal width 1st and 3rd rows - 2 columns 2nd row - 3 columns https://i.sstatic.net/aQyNR.png Order = [{ details:[ { ke ...

Angular 2's Conditional Validation: A Comprehensive Guide

Angular 2 offers straightforward validation, which is a great feature. However, the challenge lies in making a required field optional based on another field's selection. Below are the validation rules: this.contractsFilter = this.fb.group({ selec ...

Navigating the upgrade of Ag-Grid from version 21 to version 22 within an Angular 8 environment

Recently, Ag-Grid made significant changes to their code in version 22 by deploying it into modules, primarily from the new packages @ag-grid-community/all-modules or @ag-grid-enterprise/all-modules. However, the documentation on their website is quite unc ...

Exploring the functionality of this TypeScript code: What's the distinction between { [key: string]: string }[] and { prop1: string, prop2: string }[]

Below is the code I am currently working with: get tags(): { [key: string]: string }[] { let tags: { [key: string]: string }[] = []; if(this.tags) { Object.keys(this.tags).forEach(x => { tags.push({ prop1: this.tags[x], prop2: g ...

Tips for eliminating top and bottom spacing in angular material mat-row/mat-cell elements

I am working with an Angular material mat-table where I want to adjust the row height to fit only the text, without any extra space above and below it. I attempted to directly modify the mat-cell like this, but unfortunately, it did not work: <mat-cell ...

Hidden field in Angular 2 form has an invalid property

I am working with a form object called "formObject" and I am trying to store the validity status of the form in a variable called form.isFormValid. Any suggestions or assistance would be greatly appreciated. export class FormObject { public isFormValid: ...

Even after ensuring the proper type checking, I am still receiving the error message "Property 'message' does not exist on type 'object'"

I have the following code snippet: try { // api call } catch (error) { if (typeof error === 'object' && error !== null && 'message' in error) { if (typeof error.message === 'string') { if (error.me ...

Leveraging Angular, ExpressJS, and NodeJS to enable users to download a text file by simply clicking a

My goal is to incorporate a download button that allows users to download a document from my node.js server. Behold the stylish download button: https://i.sstatic.net/s4CjS.png My tech stack includes Angular for the front-end and node.js along with exp ...

What is the process of declaring a variable within a class in TypeScript?

When setting up an instance variable inside my Angular component like this: @Component({ selector: 'app-root', templateUrl: './app.component.html', //template: `` styleUrls: ['./app.component.css'] }) export class AppCo ...

Save the first and last names of a user in Firebase Auth

Greetings, amazing Stackoverflow community! We've incorporated Firebase Auth for our authentication procedures. This includes providing social sign-in options with Google and Facebook, as well as enabling users to sign in using their email and passwo ...

Unable to retrieve the third attribute of a Class using Angular2's toString method

Here is the code snippet I am working with: import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <h1>Hello {{name}}</h1> <p><strong>Email:</strong> {{email}}< ...

Update gulp configuration to integrate TypeScript into the build process

In the process of updating the build system for my Angular 1.5.8 application to support Typescript development, I encountered some challenges. After a complex experience with Grunt, I simplified the build process to only use Gulp and Browserify to generat ...