Unable to instantiate a class object in TypeScript by using the new operator

Object instantiation is occurring without any issues:

let paginationDetails: Common.Models.PaginationModel = {
    PageNumber: this.pageNumber,
    PageSize: this.pageSize,
    SearchText: this.denominationFilter,
    Ascending: true
};

However, when attempting to instantiate the object in this manner:

let pagDetails = new Common.Models.PaginationModel(
    this.pageNumber,
    this.pageSize,
    true,
    this.denominationFilter);

An error is being encountered:

https://i.sstatic.net/YszeY.png

Answer №1

Exploring the concept of an interface and its implementation can be intriguing. The code snippet at TS playground provides a clear example of this.

namespace Common.Models
{
    export interface IPaginationModel {
        PageNumber: number;
        PageSize: number;
        SearchText: string;
        Ascending: boolean;
    }
    export class PaginationModel implements IPaginationModel {
        constructor(
            public PageNumber: number,
            public PageSize: number,
            public SearchText: string,
            public Ascending: boolean
        ){}
    }
}

Utilizing the interface, we can ensure that the types align as they should. By constructing objects that adhere to the structure of IPaginationModel, we create instances with the desired properties.

// Using an Interface for type validation
let paginationParams: Common.Models.IPaginationModel = {
    PageNumber: this.pageNumber,
    PageSize: this.pageSize,
    SearchText: this.denominationFilter,
    Ascending: true
};

// Alternatively, using a class constructor
let pagParams = new Common.Models.PaginationModel(
    this.pageNumber,
    this.pageSize,
    this.denominationFilter,
    true);

// This showcases how a class can serve as an interface (similar to the first example)
let paginationParamsAsClassAPI: Common.Models.PaginationModel = {
    PageNumber: this.pageNumber,
    PageSize: this.pageSize,
    SearchText: this.denominationFilter,
    Ascending: true
};

In conclusion, interfaces and classes play pivotal roles in defining types when creating JavaScript objects. To delve deeper into this topic, refer to the detailed breakdown provided here.

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

Encountering a maximum call stack size error is a common issue when implementing d3.layout.partition in Angular

I recently developed an AngularJS directive to generate a D3 sunburst chart, but I'm encountering issues. I'm receiving a maximum call stack error in Chrome and a too much recursion error in Firefox. After some investigation, I found that the pro ...

Having difficulty accessing $scope beyond the function

One of the functions in my code is called getMail() $scope.getMail=function(){ $http.get(APISource). success(function(data) { $scope.mail =data; }). error(function(data) { console.log("error"); console.log(data); }); } In the succe ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

Get the azure blob storage file in angular by downloading it

Can anyone provide guidance on how to download a wav file from Azure Blob storage using Angular for the purpose of playing it with wavesurfer.js? ...

Angular promise not triggering loop creation

I am encountering an issue with a particular function handleFileInput(file: any) { let promise = new Promise((resolve, reject) => { this.uploadFileDetails.push({ filename:this.FileName,filetype:this.FileType}); ... resolve(dat ...

Preserve the model's key and value in the scope without saving it, thereby avoiding duplicating input fields

This is my data model: { aktuell: { N5: '8', Auftragsnr: 123 }, historie: [ { some data }, { more data } ] }; Below is the HTML code I am using: <ul> <li> <span ng-repeat="(key, value) in friends[0].akt ...

Guide to converting a string into an undefined type

I've been working on parsing a csv file: let lines = csvData.split(/\r\n|\n/); let headers = lines[0].split(','); for (let i = 1; i < lines.length; i++) { let values = lines[i].split(','); ...

Can the type definition be shared between React and AWS CDK when using Node.js?

I am currently building an application with React frontend and AWS CDK backend, using TypeScript. Due to the usage of GraphQL in certain parts of the application, I am finding myself duplicating type definitions extensively. For instance, when creating a ...

Bringing together the functionality of tap to dismiss keyboard, keyboard avoiding view, and a submit button

On my screen, there's a big image along with a text input and a button at the bottom. The screen has three main requirements: When the user taps on the input, both the input and button should be visible above the keyboard The user must be able to ta ...

Using SCSS based on the browser language in Angular: A Step-by-Step Guide

Is there a way to implement SCSS that is dependent on the user's browser language? When I checked, I found the browser language specified in <html lang = "de"> and in the CSS code as html[Attributes Style] {-webkit-locale: "en&quo ...

Angular 8 allows for dynamic updates as users provide input and make selections with check marks

When using Angular 8, if a user inputs "$10" and then clicks on it (using the OnClick event), the box below should be updated with the user input and the entire box should be selected like the example shown in the picture. I would greatly appreciate any t ...

The standard specifications for an angular component decorator's properties

Is it possible to set default properties for an angular component using a decorator? For instance, consider this component setup: @Component({ selector: 'my-component', standalone: true, host: { class: 'flx' }, changeDetection: Cha ...

Error: Module 'redux/todo/actions' could not be located. Please check the file path

Despite reading numerous posts and articles on getting absolute imports to work in a TypeScript project, I have not encountered any issues with this. My project is functioning properly with absolute imports. However, within VS Code, all absolute imports a ...

"Exploring the possibilities of combining AngularJS with PHP to manipulate

Recently, I started diving into AngularJS and encountered a situation where I needed to fetch data from a database as an array using a foreach function. Here is a snippet of the code I used: <ul> <?php foreach($Items as $Item):?> ...

Ensure that the array is completely populated before calling it in Angular

My code starts with an empty array and I need to call a service that works with the populated array. Two service calls are used to populate the array, both fetching elements from an API. The issue is ensuring the last service call waits for the array to be ...

What is the best way to change the number 123456789 to look like 123***789 using either typescript or

Is there a way to convert this scenario? I am working on a project where the userID needs to be displayed in a specific format. The first 3 characters/numbers and last 3 characters/numbers should be visible, while the middle part should be replaced with ...

Trouble updating page title in UI-Router

After trying to modify the page title in my Angular app using UI Router, I came across a demo that worked perfectly. You can check it out here. However, when I attempted to replicate the same demonstration, it did not work for me. I need to investigate wh ...

Building a MEAN stack application with socket programming, utilizing an Express app running on port 3000 and an Angular app running on port

I am working on creating a socket application using the MEAN stack and socket.io. My goal is to have my Express app running on one port (let's say 3000) and my Angular app on another port (perhaps 8000). In the future, I plan to host my Express app o ...

Troubleshooting error in Angular 5 with QuillJS: "Parchment issue - Quill unable to

I've been working with the primeng editor and everything seems fine with the editor itself. However, I've spent the last two days struggling to extend a standard block for a custom tag. The official documentation suggests using the quilljs API fo ...

Troubleshooting issues with AngularJS $watch not triggering properly

Even though Deep watch has been activated in the factory, it is not triggering. What steps can be taken to resolve this issue and ensure that an event is triggered when the 'name' value changes? Javascript Code: var app = angular.module('a ...