Base class protected properties are absent following the exclusion of another property

In my implementation, I have a generic base service that handles CRUD operations while the subclasses specify a specific entity and its endpoint URL. The base class sets a protected API URL property that each subclass initializes in the constructor.

Recently, I encountered a scenario where I needed to modify the signature of one operation for a particular entity by adding parameters to get.

To address this issue, I followed the guidance provided in this article. This approach worked, although it caused the protected API URL property to no longer exist, which has me puzzled.

To demonstrate this problem, I prepared a simplified example, which you can view interface Type1 { name: string; } interface Type2 { price: number; } class Service1 {} class BaseClass<T> { protected prop1: string = 'BaseClass'; constructor(protected dep1: Service1) {} foo(){ console.log(`${this.prop1}.foo`); } bar(){ console.log(`${this.prop1}.bar`); } } class SubClass1 extends BaseClass<Type1> { constructor(protected dep1: Service1) { super(dep1); this.prop1 = 'SubClass1'; } } type BaseClassWithoutBar = new <T>(dep1: Service1) => { [P in Exclude<keyof BaseClass<T>, 'bar'>]: BaseClass<T>[P]; }; const BaseClassWithoutBar: BaseClassWithoutBar = BaseClass; class SubClass2 extends BaseClassWithoutBar<Type2> { constructor(protected dep1: Service1) { super(dep1); this.prop1 = 'SubClass2'; // <- Error here } bar(name: string) { console.log(`${this.prop1}.bar -> ${name}`); // <- Error here } } const sub1: SubClass1 = new SubClass1(new Service1()); const sub2: SubClass2 = new SubClass2(new Service1()); sub1.foo(); sub1.bar(); sub2.foo(); sub2.bar('Bob');

To work around this issue, I could make the property public or create a new method with a different name. However, I am keen on understanding the underlying cause behind this behavior.

Answer №1

keyof operator excludes private and protected members.

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

Saving a user with a BLOB avatar in Angular 5: Tips and Tricks for Success

As a newcomer to Angular, I am trying to figure out how to properly save a new user with an avatar. Can someone help me understand how to pass the Blob value of the avatar to my user Model for successful saving? Below is the code snippet I am working with ...

A guide on connecting multiple select components to a unified Angular 6+ reactive form without triggering redundant updates

I am facing an issue where I need to connect multiple input components to a single angular reactive form, but encounter two main obstacles: By default, only the form in which user input occurs gets updated If I use [(ngModel)] it does work, but it trigge ...

Transformer Class: An object containing properties that are instances of another class

class ClassA { x: number; y: number; sum(): number { return this.x + this.y; } } class ClassB { @Type(() => ClassA) z: {[key: string]: ClassA}; } const b = transformObject(ClassB, obj); const z = b.z[key]; const s = z.s ...

NGRX 8 reducer now outputting an Object rather than an Array

I am facing an issue where the data returned from the reducer is an object instead of an array. Despite trying to return action.recentSearches, it doesn't seem to work as expected. The data being returned looks like this: { "loading": false, "recent ...

Programmatically toggle the visibility of an ion fab button

Looking for assistance in finding a method to toggle the visibility of a particular button within the collection of buttons in an ion-fab https://i.sstatic.net/vkFrP.png ...

The Power of Angular 2's Reactive Form Validation

I am currently developing a custom validator for a group of inputs within my dynamic form. this.transitionForm = this.fb.group({ effectiveStartDate: [this.utils.dateToISO(startDate), Validators.compose([Validators.required, this.validateDates])], effe ...

Utilizing the useState hook with generics in React for efficient data management

Utilizing a unique react hook designed to manage input validation for text fields and checkboxes, adaptable to both string and boolean values through the use of generics. An error is encountered when attempting to assign a value using setValue, displaying ...

Prevent TypeScript from generalizing string literals as types

I have a constant Object called STUDY_TAGS with specific properties const STUDY_TAGS ={ InstanceAvailability: { tag: "00080056", type: "optional", vr: "string" }, ModalitiesinStudy: { tag: "00080061", type: " ...

Exploring JSONPath in Cypress

I am currently working on extracting a JSON path for the specific HTML content with the language code DE Below is an example of the JSON data: { "name": "Name", "text": "", "html": "HTML content" ...

Dealing with Angular can be frustrating at times, especially when you encounter errors like "TypeError: Cannot

Encountering an Error Message... ERROR TypeError: Cannot read properties of undefined (reading 'geoCoord') at Object.next (customers.service.ts:16:38) When assigning fixed values to "lon" and "lat" variables, like 51.1634 and 10.4477, the f ...

Webpack is throwing an error due to the Vue component type being labeled as "any"

When using ts 4.2.4 and Vue3, I encountered a strange error while building my vue project: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a2a7aeaaededb3a2a0a083f3edf2edf3">[email protected]</a> build > v ...

How can you define a type in Typescript that encompasses all types that are derived from a shared type?

TLDR: How can I declare a type in Typescript that covers all types extending a specific interface? Issue Description I am working on a custom React hook that handles logic for determining if an element is being hovered over. The design is loosely based o ...

The Vuex MutationAction decorator cannot be assigned to a TypedPropertyDescriptor

I am a beginner in Typescript and apologize in advance if this is a rookie mistake. I am struggling to resolve this TS error: @Module({ namespaced: true, name: "Admin" }) class Admin extends VuexModule { public adminUserList: UserList = []; ...

Module import in Ionic

I'm encountering an issue with Ionic, Angular, and TypeScript, and I'm feeling a bit lost... I'm trying to call an external function from my file but I keep getting the following error: "Uncaught (in promise): TypeError: undefined is not an ...

the process of triggering animation to start automatically when a button is clicked in Web Development

I'm looking to create a React component that triggers an animation when clicked. I have a few options in mind: If the props are changed in the middle of the animation, it should restart the animation. The props can be changed by clicking a button on ...

The use of React hooks in an application saga led to an issue where, despite using history.push, the component

Thank you for checking out this question. I have created a login demo using react hooks and TypeScript. I have some concerns: 1. When the login is successful, I use history.push('/home') to navigate to the home page, but the page also renders a N ...

Updating button color dynamically using Angular 2

I am working on a project using angular 2 and typescript. I am looking to customize the background color of my buttons based on a variable. <div> <button md-button>1. Choose travel</button> <button md-button>2. Choose seats< ...

Tips for obtaining the iframe #document with cheeriojs?

I've been struggling to scrape the anime videos page [jkanime], specifically with extracting the mp4 video formats embedded in an iframe #document. Despite trying to use cheerio for querying, I've only managed to retrieve src links from Facebook ...

A custom function that changes the state of a nested object using a specified variable as the key argument

My goal is to update the state of an object using a function called updateDatas. This function takes the arguments key, possibly subKey, and value. interface Datas { code: number; item1: Item; } interface Item { id: number; name: string; } const ...

The button is obscured by the dropdown menu

Here is the code snippet I am working with: HTML <nav class="navbar bg-dark navbar-dark"> <div class="container-fluid"> <div class="navbar-header"> <a href="#" class=&quo ...