How can the output of a FormControl be converted into an instance of an interface?

When submitting a form contained within a FormGroup instance, I need to assign the filled values to an object created based on the interface schema. However, attempting to do this directly results in the following error:

ERROR in src/app/modules/objects/object-form/object-form-components/object-information/object-information.component.ts(97,9): error TS2322: Type '{ title: any; type_id: any; basises: any; problems: any; material_id: any; ' is not assignable to type 'ObjectFormComponent'. Object literal may only specify known properties, and 'title' does not exist in type 'ObjectFormComponent'.

const controls = this.informationForm.controls;
export interface ObjectCreateRequest {
  title: string;
  type_id: string;
  problems: string[];
  material_id: string;
}

const request: ObjectCreateRequest = {
  title: controls.title.value,
  type_id: controls.type.value.id,
  problems: controls.problems.value.map(item => item.id),
  material_id: (controls.material.value ? controls.material.value.id : null)
};

All controls within the form have any types which is invalid for the interface. How can I address this issue?

Answer №1

It appears that your error is quite straightforward if you carefully go through the error message provided. Specifically, the error states that

'title' does not exist in type 'ObjectFormComponent'
. It's important to note that the error message is not referring to the code snippet you shared - make sure to refer to the filename and line number mentioned in the error message to identify the problematic line.

Somewhere in your code, there is an object with 5 properties: title, type_id, basises, problems, and material_id. You have indicated to TypeScript that it should expect an object of type ObjectFormComponent, but instead, you provided it with a different object. The issue arises because your object contains a field named title, which is not defined in the definition of ObjectFormComponent.

Does this explanation clarify the situation for you?

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

How can we declare and showcase a generic object with an unspecified number and names of keys in React using TypeScript?

I am facing a challenge with objects that have a 'comments' field. While all the other fields in these different objects have the same types, the 'comment' field varies. I do not know the exact number or names of the keys that will be p ...

"Exploring the world of mocking module functions in Jest

I have been working on making assertions with jest mocked functions, and here is the code I am using: const mockSaveProduct = jest.fn((product) => { //some logic return }); jest.mock('./db', () => ({ saveProduct: mockSaveProduct })); ...

Struggling to make Mongoose with discriminator function properly

I seem to be facing an issue with my schema setup. I have defined a base/parent Schema and 3 children schemas, but I am encountering an error message that says: No overload match this call Below is the structure of my schema: import { model, Schema } fr ...

Guide on properly documenting custom function types in JSDoc or TypeScript to ensure accurate referencing for VSCode IntelliSense functionality

I am currently working on documenting custom function types within an object and would greatly appreciate any assistance: A Closer Look at the Issue Consider this basic object declaration with several function properties (addCoordinate, addCoordinateOne, ...

Differences between JSX.Element, ReactNode, and ReactElement: When should each be utilized?

Currently in the process of transitioning a React application to TypeScript. Everything seems to be going smoothly, however I've encountered an issue with the return types of my render functions, specifically within my functional components. In the p ...

Database records failing to update after deployment

After deploying my next js site using Vercel, I encountered an issue with the functionality related to adding, getting, editing, and deleting data from MongoDB. Although all functions were working perfectly locally, once deployed, I noticed that while I co ...

Error in TypeScript detected for an undefined value that was previously verified

I have developed a function that can add an item to an array or update an item at a specific index if provided. Utilizing TypeScript, I have encountered a peculiar behavior that is puzzling me. Here is the Playground Link. This simple TypeScript functio ...

Can you identify the category of the new Set containing the elements 1, 2, and 3?

As a beginner in TypeScript, I'm currently exploring the appropriate type for JavaScript's new Set([1, 2, 3]), but my search has been unsuccessful so far. For instance: const objNums: {[key: string]: number} = {one: 1, two: 2, three: 3}; const a ...

Encountered error: Unable to locate module - Path 'fs' not found in '/home/bassam/throwaway/chakra-ts/node_modules/dotenv/lib' within newly generated Chakra application

Started by creating the app using yarn create react-app chakra-ts --template @chakra-ui/typescript. Next, added dotenv with yarn add dotenv Inserted the following code block into App.tsx as per the instructions from dotenv documentation: import * as dote ...

Error TS2307: Module 'bluebird' not located

Currently, my focus is on developing an app using Ionic 2 and Angular 2 along with Typescript. To incorporate messaging into my app, I opted to utilize the library amqp-ts. The installation of the library through npm was successful with the following comma ...

How can I display an iframe element only if it exists within an object in Angular?

I'm currently exploring options to specifically display the iframe element within a block of HTML content being sent to my Angular application. While I can use a *ngIf directive in my template to check for the presence of the tag, I am unsure of how ...

Utilizing the ICollection interface within Angular 6

Just starting out with Angular and I could really use some guidance on how to implement an Angular Reactive Form that will generate the JSON result shown below: { "firstName":"", "middleName":"", "lastName":"", "addressBook":[{ "addressLin ...

Assign a variable to set the property of a class

Could something similar to this scenario be achievable? const dynamicPropName = "x"; class A { static propName = 1 // equivalent to static x = 1 } A[dynamicPropName] // will result in 1 or would it need to be accessed as (typeof A)[dynamicPropN ...

Typescript - ensure only one specific value is in an array of length N

Is there a way to require the 'foo' literal, while allowing the array to have any shape (i.e. not using an X-length tuple with pre-defined positions)? type requireFoo = ??? const works: requireFoo = ['bar','foo'] //This shoul ...

Implementing a Name Interface in Typescript - A Step-by-Step Guide

export declare const TerminalWidgetOptions; unique symbol; export interface TerminalWidgetOptions { endpoint: Endpoint.Options, id: string, caption: string, label: string destroyTermOnClose: boolean } Upon implementing this interface i ...

Disable and grey out the button while waiting for the Observable to broadcast successfully

component.html <button mat-raised-button color="primary" type="submit"> <mat-icon>account_box</mat-icon> <span *ngIf="!loading">&nbsp;&nbsp;&nbsp;Register</span> <span * ...

The term 'EmployeeContext' is being utilized as a namespace in this scenario, although it actually pertains to a type.ts(2702)

<EmployeeContext.Provider> value={addEmployee, DefaultData, sortedEmployees, deleteEmployee, updateEmployee} {props.children}; </EmployeeContext.Provider> I am currently facing an issue mentioned in the title. Could anyone lend a hand? ...

Enforce Immutable Return in TypeScript

Hello, I am curious to know if there is a way to prevent overwriting a type so that it remains immutable at compile time. For example, let's create an interface: interface freeze{ frozen: boolean; } Now, let's define a deep freeze function: f ...

Challenges arise when working with multiple promises while attempting to retrieve download URLs from Cloud Storage

My goal is to save each downloadURL from multiple promises (files being uploaded) in an array while iterating through them. However, what's happening is that I only get the first output for every item, regardless of how many items I upload. It keeps g ...

"Creating a visual representation of models exchanged between the client and server through Rest

Currently, I am working on a project that involves client-server communication via rest API, with Angular 2 calling restful web services as the primary method. The client side is written in Typescript, which is a subset of JavaScript. My main challenge li ...