What is the method for obtaining class constructor argument types in the form of an object?

Let's consider an example where we have a class called Summator:

export default class Summator {
   constructor(private readonly firstArgument: number, private readonly secondArgument: number) {}

   get sum() {
     return this.firstArgument + this.secondArgument
   }
}

If we use ConstructorParameters, we will get a tuple [firstArgument: number, secondArgument: number]. But how can we get an object instead of a tuple, like this:

{
  firstArgument: number,
  secondArgument: number
}

I attempted to achieve this using the following code:

[K in keyof ConstructorParameters<typeof Summator>]: ConstructorParameters<typeof Summator>[K]

However, the resulting object looks like this:

{
  0: number,
  1: number
}

Answer №1

It is not possible for arguments' wildcard names to be private (excluding the private keyword). Instead, you can declare an object as an argument in the following way:

export default class Calculator {
  private readonly firstNum: number;
  private readonly secondNum: number;

  constructor(options: {
    firstNum: number;
    secondNum: number
  }) {
    [this.firstNum, this.secondNum] = [options.firstNum, options.secondNum]
  }

  get total() {
    return this.firstNum + this.secondNum
  }
}

and

ConstructorParameters<typeof Calculator>[0]

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

Timeout error occurred in Async.js because the callback was already triggered

Whenever I execute index.js, I encounter an ETIMEDOUT or ECONNRESET error followed by a Callback was already called error. Initially, my assumption was that the issue stemmed from not including a return before calling the onEachLimitItem callback. Consequ ...

Creating a GET endpoint for a RESTful API with nodeJS

Imagine a RESTful web service where various users can share their own articles. When setting up the GET function on the server-side, it would make sense to have async functions like list, getTopViewed, getTopFavorite, and so on. So, is the following code ...

Polymer custom components and Polymer motion recognition techniques

Looking to implement a listener event on a Polymer custom element using Polymer-gestures. Here is a snippet of my code: - my-custom-element.html <link rel="import" href="../polymer/polymer.html"> <polymer-element name="my-custom-element" attri ...

Ways to have Express backend redirect you following a successful submission of a request via the Fetch API from the frontend?

Behold the following code snippet which illustrates the typical backend sign-in logic. If the user's credentials are correct, they will be redirected to the admin panel: import Express from "express"; import { Controller, Post, Body as Reque ...

Is there a way to deactivate a tab when it's active and reactivate it upon clicking another tab in Angular?

<a class="nav-link" routerLink="/books" routerLinkActive="active (click)="bookTabIsClicked()" > Books </a> I am currently teaching myself Angular. I need help with disabling this tab when it is active ...

Jsonip.com is providing both internal and external IP addresses

I'm utilizing to retrieve the IP address of users. In some cases, it is returning both an internal and external IP as a string separated by commas. I am interested in only obtaining the external IP address. Can I assume a specific order for the retur ...

What is the method for utilizing a parameter in a protractor configuration file to select specific steps?

After reading this particular question, I attempted to implement the following: protractor test/features/protractor-conf.js --params.test_set=dev_test as well as protractor-conf.js: exports.config = { // ... specs: [browser.params.test_set+'/ ...

Having trouble converting Buffered Byte Array into Image in Browser

Hello there! I am currently facing an issue while attempting to generate an image from a buffered byte array. The reason behind this is that I need to transfer the image from the server to the client using JSON. Below is the code snippet in question: The ...

What is the most effective way to design a MongoDB query that can assess a permission hierarchy involving both allow and deny rules within my Access Control

Creating a per-document access control list (ACL) for an application using MongoDB comes with specific requirements: Each find or update query can be expanded with a crafted ACL query to determine if the operation is permitted, without increasing databas ...

What could be causing the JSON String decode error to occur while utilizing extjs ajax?

Here is an example of my code: Ext.Ajax.request({ url:'test.jsp', params:{id:id,password:password}, success:function(response){ console.log(response); var results = Ext.util.J ...

Encountering an Error while uploading to a NodeJS FTP server

I am utilizing the library https://github.com/mscdex/node-ftp to transfer a file to an FTP server. Below is the code snippet that I am using: const Client = require('ftp'); console.log('CONNECTING...') const c = new Client ...

Delete any additional input forms

I have recently developed a function to dynamically add an input field to my form: function add_parameter(){ var d = document.getElementById("content"); d.innerHTML += "<br/><br><div class='custom_search col-md-5'><sp ...

Vue project encountering issue with displayed image being bound

I am facing an issue with a component that is supposed to display an image: <template> <div> <img :src="image" /> </div> </template> <script> export default { name: 'MyComponent', ...

"Unfortunately, Azure Web Static Apps do not have the capability to access .env files that are prefixed with NEXT

Suppose I have an .env file set up like this: NEXT_PUBLIC_API_BASE_PATH = value1 While this .env is functioning correctly in my local environment, once deployed to Azure Web Static Apps and added to the configurationhttps://i.sstatic.net/eqiYn.png My app ...

Tips for bringing in Cassandra driver types in TypeScript?

In the documentation for the Cassandra driver, they provide code examples like this: const Uuid = require('cassandra-driver').types.Uuid; const id = Uuid.random(); However, when attempting to use this in Visual Studio Code, the Uuid class type ...

Specific Character Count in Regular Expressions

THIS IS DISTINCT FROM THE REFERENCED QUESTION because I have extensively researched and tested various solutions before resorting to posting this inquiry. I am in need of a validation for inputs that adhere to the following criteria: The first character ...

On my production server, json_encode functions flawlessly, however, in the development environment, it

I'm stumped on how to solve this issue. Appreciate any help. My json_encode function works perfectly in the development environment, but it's failing on my production server. ...

Tabulator's seamless text wrapping feature

var table = new Tabulator("#example-table", { layout: "fitColumns", //adjust columns to fit table width height: "69vh", responsiveLayout: "collapse", //hide columns that don't fit in the table cellVertAlign: "middle", headerSort: false, c ...

Is there a way I can modify the display setting to show 4 slides per view?

var swiper = new Swiper(".new-arrival", { slidesPerView: 4, centeredSlides: false, spaceBetween: 30, autoplay: { delay: 5500, disableOnInteraction: false, }, pagination: { el: ".swiper-pagination", type: &qu ...

What is the most efficient way to extract dynamically generated JavaScript content from a webpage?

Currently, my method involves using selenium alongside PhantomJS to scrape dynamic content generated by JavaScript on a website. Although it provides me with the desired results, the process is quite slow as I have to wait for the entire page to load befor ...