ts1109: An error occurred as there was an expectation for an angular

I am encountering an error while creating a simple form with Angular using a reactive form. I'm puzzled as to why it's indicating that something is missing:

Although I have created forms numerous times before, this is the first instance of such an error.

https://i.stack.imgur.com/Vnyj0.jpg

ts.file

  productForm: any = FormGroup;

  constructor(private fb: FormBuilder,
    public dialogRef: MatDialogRef<DialogFormComponent>, 
    @Inject(MAT_DIALOG_DATA) public data:any) { }

  ngOnInit() {
    this.initDialogForm();
  }

  initDialogForm() {
    this.productForm = this.fb.group({
      id: [this.data?.id],
      name: [this.data?.name , Validators.required],
      price: [this.data?.price , Validators.required],
      comment: [this.data?.comment , Validators.required],
      date : [this.data?.date]
    });
  }

  onSubmit() {
    this.dialogRef.close(this.productForm.value);
  }

}

html

mat-dialog-content>
    <form [formGroup]="productForm" (ngSubmit)="onSubmit()">
        <div class="from-group">
            <label for="name">Nom</label>
            <input id="name" type="text" formControlName="name" class="form-control">
        </div>
        <div class="from-group">
            <label for="price">Prix</label>
            <input id="price" type="number" formControlName="price" class="form-control">
        </div>
        <div class="from-group">
            <label for="comment">Commentaire</label>
            <input id="comment" type="text" formControlName="comment" class="form-control">
        </div>
        <div class="from-group">
            <label for="expiration">date de péremption</label>
            <input id="expiration" type="date" formControlName="date" class="form-control">
        </div>
        <mat-dialog-actions>
            <button>Valider</button>
        </mat-dialog-actions>
    </form>
</mat-dialog-content>

Answer №1

? signifies the conditional operator used inside formgroup, which is why the compiler perceives that a missing condition exists after :

   In this example, we see how 'this.productForm' is assigned values using FormBuilder in Angular:
   
   this.productForm = this.fb.group({
      id: [this.data.id],
      name: [this.data.name , [Validators.required]],
      price: [this.data.price , [Validators.required]],
      comment: [this.data.comment , [Validators.required]],
      date : [this.data.date]
    });

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

Creating an Array in TypeScript

Is it possible to declare a global array in Typescript so that it can be accessed using "this" from any part of the code? In javascript, I would typically declare it as "var anArray=[]". What is the equivalent way of doing this in Typescript? Using anArra ...

Creating a universal wrapper function to serve as a logging tool?

Currently, I am working on a generic JS function that can wrap any other function. The purpose of this wrapper is to execute the wrapped function, log the input and output events, and then return the output for "transparent" logging. However, as I attempt ...

What is the correct way to incorporate service within a component function?

Here's how I'm implementing an inline input edit component: <app-inline-input-edit [label]="'Manufacturer'" [required]="true" [(ngModel)]="ctrlTypes.manufacturer" name="manufacturer" [changed]="onChange"> ...

Encountering obstacles when attempting to initiate a node application due to an error

Upon starting my node application, I encountered an error. The error message reads: "ERROR in Metadata version mismatch for module ../node_modules/@ng-bootstrap/ng-bootstrap/index.d.ts, found version 4, expected 3" Here is the full error trace: ERROR ...

Angular 16 routing not loading content

I configured the routes in Angular v16 and encountered a blank page issue with the login and register functionality. I suspect it has to do with routing, but after checking multiple times, I couldn't pinpoint the exact problem. Below are snippets of t ...

Angular's isteven-multi-select Component: A Versatile Selection Tool

Is it possible to utilize isteven-multi-select with ng-model? My goal is to have certain items appear as chosen when the page loads using the isteven-multi-select module. I've tried using ng-model in the HTML page to populate the isteven-multi-select ...

React doesn't have file upload configured to update the state

I am working on integrating a file upload button that sends data to an API. To ensure only the button triggers the upload dialog and not the input field, I have set it up this way. Issue: The File is not being saved to state, preventing me from using a ...

Optimizing font loading with angular CLI

EDIT: AFAIK This is a unique question and not related to Webpack disable hashing of image name on output because: The webpack.config file is no longer editable in the latest versions of Angular CLI. I actually want to keep the hash on the fon ...

The Karma ng test fails to display any build errors on the console

I've encountered a frustrating issue while working with Karma and Jasmine for testing in my Angular 7 project. The problem arises when there are errors present in the .spec.ts or .ts files, as running ng test does not display these errors in the conso ...

When the async keyword is added, the return type in Typescript can vary

This situation is really puzzling to me. I wrote a function to calculate the number of documents in a collection getDocCount(): Promise<number> { return MyModel.countDocuments({}); } Everything seemed fine. However, when I removed async since I ...

Discovering class methods in typescript

Currently, I am running TypeScript unit tests using Mocha Chai after configuring the compiler options to ts-node. Within one of my unit tests, I am seeking a way to retrieve all methods from a utility class that I have designed and execute the same set of ...

Why does using `withCredentials: true` and including a `body` in the request cause a CORS error in Angular HttpClient?

My objective is to make a request to a Cloud Function, receive a response with a Set-Cookie header, and have the browser store the cookie. The issue arises when the response containing a Set-Cookie header is ignored without the presence of withCredentials ...

Transform event binding using PrimeNG Elements and Angular

My challenge lies in dynamically binding change events within the PrimeNG TreeTable by defining functions on my columns. I've noticed that attempting to bind the change event dynamically with my columns doesn't seem to work properly inside the Tr ...

Can Angular's change detection be triggered by a change in @Input?

As of now, I am immersing myself in guides and tutorials on Angular's change detection. There are some statements that are quite perplexing to me. Therefore, I am seeking confirmation or clarification. The default Change Detection is activated "every ...

Displaying a TypeScript-enabled antd tree component in a React application does not show any information

I attempted to convert the Tree example from antd to utilize TypeScript, however, the child-render function does not seem to return anything. The commented row renders when I remove the comment. The RenderTreeNodes function is executed for each element in ...

Error: The variable "prisma" is not defined in this context - Next.js version 14

While working with Prisma and next.js 14, I encountered an issue with the Stripe payment API. The error message ReferenceError: prisma is not defined popped up. How can I resolve this? import { NextApiRequest, NextApiResponse } from "next" import ...

Is it possible to utilize a single node_modules folder for multiple Angular 2/4 projects?

As I dive into the world of Angular, I've noticed that creating a new project can be time-consuming due to the 100MB "node_modules" folder that the CLI generates. The contents of this folder are repetitively consistent across projects, with few change ...

Issue with PrimeNG Calendar month picker functionality not functioning as expected

I recently integrated a PrimeNG code snippet for a month picker. Although it is functioning correctly, I noticed a discrepancy compared to the PrimeNG example - specifically, the year does not change when clicking on the arrow buttons. The ngModel, howev ...

The data type '{ [key: string]: number; }' cannot be assigned to type 'T'

I’m working with a complex TypeScript type and trying to manage it within a function. Here’s what I have: type T = { a: number } | { b: number } function func(k: 'a' | 'b', v: number) { // error message below const t: T = { ...

Angular 10: Display a notification when all checkboxes have been ticked

I am currently working on a project that involves Angular 10. The initial requirement was as follows: Users are presented with a list from which they need to choose one option using radio buttons. For example: the user selects covid19 from the given lis ...