Is the Inline Partial<T> object still throwing errors about a missing field?

I recently updated to TypeScript version ~3.1.6 and defined an interface called Shop as follows:

export interface Shop {
  readonly displayName: string;
  name: string;
  city: string;
}

In this interface, the property displayName is set by the backend and cannot be altered by the user interface.

To create a new Shop instance, I implemented a Service that makes a REST call with properties city and name:

createShop = (shop: Partial<Shop>) =>
  this._http.post(`/shop/new`, shop, this._options)
    .pipe(
      catchError(this.handleError)
    );

When passing data from an Angular 7 reactive form, it looks like this:

createShop = () =>
  this._shopService.createShop({
    name: this.form.get('name').value,
    city: this.form.get('city').value
  }).subscribe(/* handlers */);

I used the type Partial<Shop> for the create method's shop parameter to avoid creating a separate <CreateShop> interface, assuming that was the purpose of Partial<T>.

However, during compilation, I encountered this error:

src/app/addShop.component.ts(39,40): error TS2345: Argument of type '{ name: string; city: string; }' is not assignable to parameter of type 'Shop'.
  Property 'displayName' is missing in type '{ name: string; city: string; }'.

By defining a new interface CreateShop which extends Partial<Shop>, I was able to resolve this compilation issue.

Why am I unable to use inline declaration for Partial?

Especially when it goes against what is suggested in Partial<T> only works with inline values

Answer №1

To resolve this problem, simply convert the object literal to type Shop

createShop = () =>
  this._shopService.createShop({
    name: this.form.get('name').value,
    city: this.form.get('city').value
  } as Shop).subscribe(/* handlers */);

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

Steps for creating a TypeScript project with React Native

Hey there, I'm just starting out with react-native and I want to work on a project using VS Code. I'm familiar with initializing a project using the command "react-native init ProjectName", but it seems to generate files with a .js extension inst ...

Ensure your TypeScript class includes functionality to throw an error if the constructor parameter is passed as undefined

My class has multiple parameters, and a simplified version is displayed below: class data { ID: string; desp: string; constructor(con_ID:string,con_desp:string){ this.ID = con_ID; this.desp = con_desp; } } When I retrieve ...

Determining the appropriate version of the types package for your needs

It has come to my attention that certain npm packages do not come with types included. Because of this, the community often creates @types/packagename to provide those types. Given that both are packages, how does one determine which version of the types ...

Determining the data type of an object key in Typescript

Is there a way to limit the indexed access type to only return the type of the key specified? interface User { id: string, name: string, age: number, token: string | null, } interface Updates<Schema> { set: Partial<Record< ...

The parameter 'unknown[]' cannot be assigned to the type 'OperatorFunction'

component.ts initialize() method explains The error message says 'Argument of type 'unknown[]' is not assignable to parameter of type 'OperatorFunction<unknown[], unknown>'. Type 'unknown[]' does not match the si ...

What is the best way to set State conditionally based on two different objects, each with its own type, without resorting to

I am trying to create two different objects, each with slightly different types, in order for them to be compatible with a state object that contains values of both types. However, I am encountering the following error: Property 'dataA' does no ...

Creating a variety of themes with unique color palettes for Angular Material along with custom-designed components

One of the goals for my app is to have multiple themes, including Angular Material themes, with the ability to define custom colors for specific components and elements that are not part of Angular Material. It's important that when I change a theme, ...

Issue "unable to use property "useEffect", dispatcher is undefined" arises exclusively when working with a local npm package

I am currently in the process of creating my very own private npm package to streamline some components and functions that I frequently use across various React TypeScript projects. However, when I try to install the package locally using its local path, ...

How to retrieve TypeScript object within a Bootstrap modal in Angular

Unable to make my modal access a JavaScript object in the controller to dynamically populate fields. Progress Made: Created a component displaying a list of "person" objects. Implemented a functionality to open a modal upon clicking a row in the list. ...

Issue encountered during execution of tests involving reactive forms

Having some issues with the code below and looking for a solution. The tests pass when mocking the form for ts tests, but encounter an error when mocking the same form for html: No value accessor for form control with name: 'Enable' If I remov ...

The object filtering process is experiencing issues due to the presence of a null value in the column

I am trying to extract object data based on a specific value from an array. While the code snippet below works well when there are no null values, it fails to work properly when encountering null values in the column. For reference, you can check out this ...

Unlocking Not Exported Type Definitions in TypeScript

Take a look at this TypeScript code snippet: lib.ts interface Person { name: string; age: number; } export default class PersonFactory { getPerson(): Person { return { name: "Alice", age: 30, } } } ...

Unable to utilize material tabs in this situation

Discovering the material tabs feature at https://material.angular.io/components/tabs/api#MatTab got me excited to implement it in my project. After adding the suggested import, I encountered an issue where I couldn't find the module "@angular/materia ...

Does the React memo function modify the component's prop type?

I've come across a strange issue where defining two components causes compilation errors when written separately but not when written in the same file. test3.tsx import React from "react"; type ValueType = number[] | string[] | number | st ...

Encountered an issue while integrating CKEditor 5 into a standalone Angular 17 application: "Error: window is

Having trouble integrating CKEditor with my Angular app (version 17.1.2 and standalone). I followed the guide step by step here. Error: [vite] Internal server error: window is not defined at r (d:/Study/Nam3_HK3/DoAn/bookmanagement/fiction-managemen ...

Adding multiple classes to a condition in Angular can be achieved by using the ngClass directive

Here is the code snippet I am working with: <div *ngFor="let d of getDays(); let i = index" class="day hr left" (click)="pick(d, h)" [class.white]="days[d].indexOf(h) > -1"> </div> Everything seems to be working well, but I have a ques ...

What is the best way to manage a debounced event emitter in Cypress using RxJs debounceTime?

My task involves creating tests for a web page containing a list of items and a search-filter feature. The search-filter functions by filtering the list of items based on the input entered into its text field. Whenever the value in the text field changes, ...

When using the Ng --version command on a development package, it throws an error

I encounter an error with a development package when cloning a repository. I would greatly appreciate any advice on how to resolve this issue. https://i.stack.imgur.com/DBp5r.png ...

What is an improved method for defining a TypeScript type to store API method invocations?

Currently, I am exploring ways to enhance either GenericActionables or Items in a way that eliminates the need to manually add the API method name as a GenericActionables<"nameOfNewMethod"> to the Actionables type every time. Any suggesti ...

Can we find a superior method?

I am currently using a stateful service to retrieve questions. The service checks if the questions exist in the local state, and if they do, it returns them. Otherwise, it makes an HTTP call to the API to fetch and store the questions. The service method ...