With TypeScript ~3.1.6, I have declared this interface:
export interface Shop {
readonly displayName: string;
name: string;
city: string;
}
where displayName
is set by the backend and cannot be changed by the UI.
I created the following Service to create a new Shop, the REST for which takes city
and name
as properties:
createShop = (shop: Partial<Shop>) =>
this._http.post(`/shop/new`, shop, this._options)
.pipe(
catchError(this.handleError)
);
And I pass data (from an Angular 7 reactive form) thusly:
createShop = () =>
this._shopService.createShop({
name: this.form.get('name').value,
city: this.form.get('city').value
}).subscribe(/* handlers */);
I've declared the create method's shop as a Partial<Shop>
because I don't want to maintain a separate <CreateShop>
interface, and I thought this was precisely what Partial<T>
s were for.
Yet, I still get this error at compilation:
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; }'.
If I declare a
export interface CreateShop extends Partial<Shop> {}
and use that instead of Partial<Shop>
it compiled.
Why can't I use the inline Partial
declaration?
Especially as that seems to go exactly against Partial<T> only works with inline values