Received 2 arguments instead of the expected 1 in the custom validator causing an error (ts 2554)

After implementing the following validator, I encountered an error message.

The error states: "Expected 1 argument, but got 2 (ts 2554)."

Although many sources mention overloading as a common issue, there is no overload present in this case.

export const customValidatorAsync = (co: CustomObject, id: string): AsyncValidatorFn =>
return (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => {
    ...
});

When incorporating it like so (this field is within a group of fields that users can add instances to):

setFieldValidators(index: number): void {
    const array = <FormArray>objectRegistrationForm.controls["children"];
    const group = <FormGroup>array.controls[index];
    group.controls["field"].setValidators([
    Validators.required,
    Validators.maxLength(20)],
    [customValidatorAsync(customObject, objectRegistrationToSave.children[index].id)]);
}

Previously, this validator did not require the id since user data was non-modifiable. However, currently, the data needs validation if it's being used by another object. Therefore, the current id is necessary (it will be empty when creating a new registration, allowing for a possible null value).

I have verified that the expected data is being received, but I am perplexed as to why this validator is now generating an error. When hovering over the import statement, it insists on two arguments instead of one.

Answer №1

It turns out I had been focusing on the wrong aspect...

setFieldValidators(index: number): void {
    const array = <FormArray>objectRegistrationForm.controls["children"];
    const group = <FormGroup>array.controls[index];
    group.controls["field"].setValidators([
    Validators.required,
    Validators.maxLength(20)],
    [customValidatorAsync(customObject, objectRegistrationToSave.children[index].id)]);
}

The issue lies in having an extra pair of brackets ] before the custom validator in setValidators.

setFieldValidators(index: number): void {
    const array = <FormArray>objectRegistrationForm.controls["children"];
    const group = <FormGroup>array.controls[index];
    group.controls["field"].setValidators([
    Validators.required,
    Validators.maxLength(20),
    customValidatorAsync(customObject, objectRegistrationToSave.children[index].id)]);
}

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

Unable to utilize the web-assembly Rust implementation due to the error stating 'Cannot access '__wbindgen_throw' before initialization'

Looking to integrate some web-assembly into my project, I started off by testing if my webpack setup was functioning properly and able to utilize my .wasm modules. Here's a snippet of what I came up with: #[wasm_bindgen] pub fn return_char() -> cha ...

how to verify if a variable exists in TypeScript

Is there a recommended method for verifying if a variable has a value in TypeScript 4.2? My variable may contain a boolean value. I'm thinking that using if(v){} won't suffice as the condition could be disregarded if it's set to false. ...

Is it a good idea to separate TypeScript types into their own package?

In my React/TypeScript application, I have approximately 100 files where various types are declared. I am looking for a simpler and more automated approach to extract all these types into a separate package. Is there a method other than manually copying ...

Enforcement of Typescript Field Type Lax During Assignment

My current issue involves a constructor that is supposed to set the value of _device. The field is specifically designed to be of type number, and the constructor parameter is also expected to be of type number. However, when a parameter of type string is ...

Acquire the URL using Angular within a local environment

I am currently working on a basic Angular project where I have a JSON file containing some data. [{ "name": "Little Collins", "area": "Bronx", "city": "New York", "coverImage": "https://images.unsplash.com/photo-1576808597967-93bd9aaa6bae?ixlib=rb-1.2.1&a ...

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 ...

A method for handling specific subsets of an enum in a secure and controlled manner

I have an enumerated type called Country and another type that represents a subset of European countries. I want to handle values within this subset differently from others. Currently, I am using an if statement with multiple conditions, but it could get u ...

What Causes a Mongoose Query to Result in an Empty Array?

Hello, I have reviewed similar questions regarding the issue I am facing with developing an API. Despite trying different solutions, none seem to resolve my problem. When handling request and response payloads in my API, everything seems to be working fin ...

Controlling numerous websockets using React

I am currently developing a single-page application using React.js with a JSON RPC 2.0 backend API that relies on websockets. Managing multiple websocket connections simultaneously across different React.js components has been a challenge. Initially, I th ...

Executing axios calls within other axios calls and altering state in React before all calls have completed

Currently, I am working on implementing nested axios calls to create the desired object by making multiple API requests. However, I am facing an issue where the state updates before all requests have finished, causing my table to populate entry by entry ...

Animate in Angular using transform without requiring absolute positioning after the animation is completed

Attempting to incorporate some fancy animations into my project, but running into layout issues when using position: absolute for the animation with transform. export function SlideLeft() { return trigger('slideLeft', [ state('void&a ...

Add the slide number and total count in between the navigation arrows of the owl carousel

In my Angular application, I am utilizing an ngx owl carousel with specific configurations set up as follows: const carouselOptions = { items: 1, dots: false, nav: true, navText: ['<div class='nav-btn prev-slide'></div>' ...

Deactivate the rows within an Office UI Fabric React DetailsList

I've been attempting to selectively disable mouse click events on specific rows within an OUIF DetailsList, but I'm facing some challenges. I initially tried overriding the onRenderRow function and setting CheckboxVisibility to none, but the row ...

What are the steps to avoid TypeScript from automatically installing and referencing its own @types in the AppDataLocal directory?

I'm encountering a perplexing issue where it appears that TypeScript is setting up its own version of React in its unique global cache system (not entirely sure what to call it? presuming that's the case) and utilizing it within my project. In p ...

Enhance your material-ui component using TypeScript functionality

Exploring ways to enhance the Material-ui Button component by introducing new props. The objective is to introduce a new prop called fontSize with three size options - small, medium, large. <Button variant="outlined" color="primary" ...

Issues with showing data in Angular Material tables

I recently deployed my Angular Application on a server and encountered an issue with the Angular Material table. Despite data being present in the logs, it does not display on the table. This problem only occurs in production, as everything works perfectl ...

Encountering an issue with core.js:15723 showing ERROR TypeError: Unable to access property 'toLowerCase' of an undefined value while using Angular 7

Below, I have provided my code which utilizes the lazyLoading Module. Please review my code and identify any errors. Currently facing TypeError: Cannot read property 'toLowerCase' of undefined in Angular 7. Model Class: export class C_data { ...

Changing the environment variable in an Angular application with user input

I'm currently in the process of developing an angular application that interacts with a REST API on the backend server. The URL for this server is currently set as an environment variable like so: export const environment = { production: false, lo ...

Access PDF document in a fresh tab

How can I open a PDF file in a new tab using Angular 6? I have tried the following implementation: Rest controller: @RestController @RequestMapping("/downloads") public class DownloadsController { private static final String EXTERNAL_FILE_PATH = "/U ...

What is the best way to verify numerical input from scanf()?

Can someone help me with a question I have regarding linked lists? My data type is int, and I need to figure out how to validate it so that it doesn't accept alphabetic input. The current method I'm using isn't displaying the error message ...