typescript error: argument type is not assignable to parameter of type

Karavana

I am trying to get rid of this erro message and trying to have my code work. My intention is to create an angular form validation consisting of firstName lastName businessName. If firstName and lastName are filled, businessName is no longer required and vice versa. If 'business' segment is selected, onSubmit, we need to clear firstName and lastName. If 'personal' is selected', onSubmit clear 'businessName'. My app.component.html gives this error in StackBlitz environment.

Argument of type 'Event' is not assignable to parameter of type 'CustomEvent<any>'.
Type 'Event' is missing the following properties from type 'CustomEvent<any>': detail, initCustomEvent

My segment is not in form but it's in the toolbar ion-segment. Here is a snippet and my code. Do I have any structural issues maybe? I can't tell maybe it is too simple. Help me out please.

Edit: Let me also provide you my development environment if it is appropriate stackblitz

//app.component.ts file
import { Component, OnInit } from '@angular/core';
import {
  FormBuilder,
  FormGroup,
  Validators,
  AbstractControl,
  ValidationErrors,
} from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./global_styles.css'],
})
export class AppComponent implements OnInit {
  form!: FormGroup;
  segmentValue = 'personal';

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.form = this.formBuilder.group({
      firstName: [''],
      lastName: [''],
      businessName: [''],
    });

    this.form.setValidators([this.customValidator.bind(this)]);
  }

  customValidator(control: AbstractControl): ValidationErrors | null {
    const formGroup = control as FormGroup;
    const firstName = formGroup.get('firstName')!.value;
    const lastName = formGroup.get('lastName')!.value;
    const businessName = formGroup.get('businessName')!.value;

    if (this.segmentValue === 'business' && !businessName) {
      return { businessNameRequired: true };
    }

    if (this.segmentValue === 'personal' && (!firstName || !lastName)) {
      return { namesRequired: true };
    }

    return null;
  }

  segmentChanged(event: CustomEvent) {
    this.segmentValue = event.detail.value;
    this.form.updateValueAndValidity(); // To trigger validation when segment changes
  }

  onSubmit() {
    if (this.segmentValue === 'business') {
      this.form.get('firstName')!.reset();
      this.form.get('lastName')!.reset();
    } else {
      this.form.get('businessName')!.reset();
    }
    // Proceed with form submission
    console.log(this.form.value);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>

<?-- app.component.html file ?>

<ion-toolbar>
  <ion-segment
    [(ngModel)]="segmentValue"
    (ionChange)="segmentChanged($event as CustomEvent)"
  >
    <ion-segment-button value="business"> Business </ion-segment-button>
    <ion-segment-button value="personal"> Personal </ion-segment-button>
  </ion-segment>
</ion-toolbar>

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <div *ngIf="segmentValue === 'personal'">
    <label for="firstName">First Name:</label>
    <input type="text" id="firstName" formControlName="firstName" />
    <div
      *ngIf="form.hasError('namesRequired') && form.get('firstName').touched"
    >
      First Name is required.
    </div>
  </div>

  <div *ngIf="segmentValue === 'personal'">
    <label for="lastName">Last Name:</label>
    <input type="text" id="lastName" formControlName="lastName" />
    <div *ngIf="form.hasError('namesRequired') && form.get('lastName').touched">
      Last Name is required.
    </div>
  </div>

  <div *ngIf="segmentValue === 'business'">
    <label for="businessName">Business Name:</label>
    <input type="text" id="businessName" formControlName="businessName" />
    <div
      *ngIf="
        form.hasError('businessNameRequired') &&
        form.get('businessName').touched
      "
    >
      Business Name is required.
    </div>
  </div>

  <button type="submit">Submit</button>
</form>

Chellappan வ

If you have typescript strict mode enabled in your angular project, then you can't pass segmentChanged(event: CustomEvent) because It is not considered valid instance of EventListener.

One way to fix this issue by type assertion

segmentChanged(event: Event) {
    this.segmentValue = (event as CustomEvent).detail.value;
    this.form.updateValueAndValidity(); // To trigger validation when segment changes
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript error 'argument of type is not assignable to parameter of type'

Argument of type is not assignable to parameter of type Typescript Error

Typescript error: Argument of type 'Object' is not assignable to parameter of type '{}[]'

TypeScript: Argument of type '(error: any) => void' is not assignable to parameter of type

Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type

Typescript enums typing error (argument of type 'string' is not assignable to parameter of type)

CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"

Typescript error: Argument of Type X is not assignable to parameter of type Y

Typescript Error Argument of type 'string | number' is not assignable to parameter of type 'never'

Typescript, useContext(), error: Argument of type 'never[]' is not assignable to parameter of type

React Typescript - Argument of type is not assignable to parameter of type

useRecuder + typescript error Argument of type '(type, action) => { state: (...}' is not assignable to parameter of type 'ReducerWithoutAction<any>'

Typescript React type argument of type not assignable to parameter of type

Error Argument is not assignable to parameter of type 'AxiosRequestConfig'

typescript fix error argument of type is not assignable

Typescript error TS2345 Error: TS2345:Argument of type 'Buffer' is not assignable to parameter of type 'string'

Argument of type is not assignable to parameter of type

Typescript Error: Argument of type 'number' is not assignable to parameter of type 'Expected<Promise<number>>'

Error on drag and drop action on protractor typescript, Argument of type '"{x: 540, y: 504}"' is not assignable to parameter of type 'ILocation'

typescript error: Argument of type 'any' is not assignable to parameter of type 'never' - in accessing specific key in array

How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>'

ReactJS Typescript Argument of type '{ keyPrefix: string; }' is not assignable to parameter of type string

Argument of type 'string' is not assignable to parameter of type 'never' in typeScript

TypeScript Argument of type 'IAccount' is not assignable to parameter of type 'never'

Typescript: Argument of type 'number' is not assignable to parameter of type 'string'

typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345)

React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type

Typescript keyboard event : argument of type 'Event' is not assignable to parameter of type 'KeyboardEvent'

Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript