#Odd Behavior (Angular, Forms)

2 messages · Page 1 of 1 (latest)

iron rune
#

My best guess is something to do with how I'm somewhat abusing Partial to allow my service to accept the form data, since I didn't want my controller directly creating a wishlist, but rather my service.

    const wishlist: Partial<Wishlist> = {
      name: this.wishlistForm.value.name!, // I use ! to disallow `null` typing, which Partial doesn't like
      description: this.wishlistForm.value.description!, // ^
    };
#

ChatGPT goated...

  /**
   * Adds a wishlist to the list of wishlists
   * @param wishlist Wishlist
   */
  addWishlist(wishlist: Partial<Wishlist>): void {
    const id = this.wishlists.length + 1;
    const name = wishlist.name || 'New Wishlist';
    const description = wishlist.description || 'New Description';

    const newWishlist: Wishlist = {
      id,
      name,
      description,
    };

    this.wishlists.push(newWishlist);
  }