Can inheritance pass constructor values in TypeScript/JavaScript?

I stumbled upon some code that seems confusing to me: I am still relatively new to TypeScript, but this doesn't seem like it should be functioning correctly:

There are 2 classes involved in this scenario (pertaining to an automation framework, specifically playwright): a Base page and a Home page that inherits from the base page.

Base Page:

import type { Page } from "@playwright/test";

export abstract class BasePage {
  constructor(public readonly page: Page) {}

  async open(path: string) {
    await this.page.goto(path);
  }
}

Home Page:

import { BasePage } from "../base.page";

export default class HomePage extends BasePage {
  public readonly HomePageButton = this.page.getByTestId("home-button");

  async open() {
    await super.open("/");
  }
}

When I try to implement something similar in my own code, it results in errors. Specifically, how does the HomePage know what this.page refers to? Wouldn't we need to create a new constructor and call super(page)?

I understand that TypeScript will generate "Parameter properties," but is there a way to make VSCode recognize it? If the approach of declaring child classes of an abstract base class is incorrect, what would be the recommended alternative?

Answer №1

The HomePage utilizes the constructor from the BasePage.

In order to instantiate an instance of the HomePage class, you must provide the page parameter in the constructor.

This allows you to access this.page from the parent class within the child class.

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

"Troubleshooting a case where mongoDB's updateOne function is

I am in the process of removing certain references to objects within different sections (section1, section2, section3, others) in each file. Sample Document: { "_id": "64a3268474aa29e72b40c521", "name": "Test", ...

reactjs: disable a specific eslint rule

I'm trying to figure out how to disable the "no-unused-vars" rule specifically for TypeScript interfaces. Here's a code snippet where I'm getting a warning: export type IKeoTableColumn<T> = { id: T; align: 'left' | ' ...

What is the best way to include a hyperlink in a component?

Is there a way to link this code snippet: <Col key = {item.id}> <StoreItem {...item}/> </Col> to redirect to the following route? <Route path = '/' element = {<Home />} /> Here is the source co ...

Developing a Angular 2.3 custom library with advanced dependency injection techniques

I am currently facing a challenge in integrating a custom service from a Yeoman-created library into my existing Ionic2 project. The index.ts file of the library, which will be installed as an npm module, is structured as follows: @NgModule({ imports: ...

Adding onBlur validation for radio buttons and checkboxes in Angular Material UI

Currently, I am working on implementing checkboxes and radio buttons using Angular Material UI. My main issue lies in achieving the desired red outline effect when a required field is left unselected by the user. Even after applying the necessary 'req ...

Typescript input event

I need help implementing an on change event when a file is selected from an input(file) element. What I want is for the event to set a textbox to display the name of the selected file. Unfortunately, I haven't been able to find a clear example or figu ...

Extracting a field from an array of objects in Angular is a powerful feature

I'm feeling a bit confused and would appreciate some explanations please! In my code, I have a datatable that generates an array of Operation objects in my .ts file. Here is the object class: export class Operation { id: number; name: string; } ...

Issue with IN operator functionality in TypeORM when used with MongoDB

My goal is to fetch a list of items using an array of IDs by utilizing the following code: import { In } from 'typeorm'; ...findBy({ _id: In(ids) }) The IDs are predefined upon creation: @Entity() export class Foo { @ObjectIdColumn({ generated ...

Subscribing to valueChanges in reactive forms to dynamically update checkbox options

My goal is to create a select dropdown with options for bmw, audi, and opel. The user can only select one option from the dropdown, but they should also have the ability to select the options that were not chosen using checkboxes. cars = [ { id: 1, na ...

Is VSCode disregarding tsconfig.json and compiling individual files, causing misleading error messages to appear?

After updating typescript, angular, and all the libraries in my project, I encountered a new issue that was not present before. Although I successfully ensured that my code builds without any errors or warnings from the command line, Visual Studio Code sta ...

Presentation of information with loading and error scenarios

How can we effectively display data in an Angular view, considering loading state and error handling? Imagine we are fetching a set of documents from our backend and need to present them in an Angular view. We want to address three possible scenarios by p ...

Building an AngularJS Service with TypeScript that is Non-Singleton: A Step-by-Step Guide

I need help converting an AngularJS Typescript Service into a Non-Singleton. Can anyone provide guidance on how to achieve this? (Note: This is not the same as other questions that focus on achieving this in JS) I have included some simple pseudo code be ...

With Node.js Express, it is impossible to establish a route within an inherited class

There is a Controller.js file: var Controller = function () { } Controller.prototype.getAll = function (req , res) { console.log(this.model); } module.exports = Controller; Also, there is StickerController.js: var Controller = require('./Abst ...

The version of the replication configuration schema does not support the use of ReplicationTime

I am currently working on setting up S3 Replication using the AWS CDK. I have referenced https://github.com/rogerchi/cdk-s3-bucketreplication/blob/main/src/index.ts as a starting point, and while it does create a replication rule, I am facing some issues c ...

Instructions for activating the "Navigate to Declaration" feature in TypeScript for JSON files using i18next

Currently, I am actively engaged in a project that involves the use of i18next with react and typescript. In this project, translation keys are defined within .json files. However, a notable drawback of transitioning to json for the translation files is l ...

Every time a new message is sent or received, I am automatically brought back to the top of the screen on the

I'm currently working on integrating a chat feature into my Angular Firestore and Firebase app. Everything seems to be functioning well, except for one issue - whenever a new message is sent or received, the screen automatically scrolls up and gets st ...

Awaiting the completion of Promises within a for-loop (Typescript)

I'm struggling with a for-loop and promises in my angular2 project. I have multiple methods that return promises, and after these promises are resolved, I want to populate an array in the class using Promise.all(variable).then(function(result){....... ...

What is the process for removing a specific column (identified by its key value) from a JSON table using JavaScript and Typescript?

[{ "name": "employeeOne", "age": 22, "position": "UI", "city": "Chennai" }, { "name": "employeeTwo", "age": 23, "position": "UI", "city": "Bangalore" } ] If I remove the "Position" key and value from the JSON, the updated r ...

Tips for recursively updating a value with javascript functions

Given an array object listarray and passing updatevalue as a parameter, Note: The first larger value will be the first greater value than updatevalue Update the value of updatevalue to the first larger value, then increment the second larger value by add ...

Error: Unable to locate bundle.js when attempting to refresh the page with a specific ID in the URL

I encountered an issue where I tried redirecting a user to their profile page to display the profile information corresponding to it. Let's say we have http://localhost:8080/user/1 as an example. Upon redirecting the user using the navbar link, the pa ...