Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11781

How to correctly type Object.entries to my custom type?

$
0
0

I've these 3 fields in my form (sku, sku_variation, name) and I want to use them to create a new product.

I came up with this solution recasting the parsedData to unknown first but it looks completely wrong.

  export type TProduct = {    id: string,    sku: number,    skuVariation: number,    name: string,  }  export type TCreateProduct = Omit<TProduct, 'id'>;  const createProduct = async(parsedData:TCreateProduct) => {    fetch('http://localhost:3333/api/products/',{      method: 'POST',      body: JSON.stringify(parsedData)    })    return '';  }  const handleSubmit = (e:React.FormEvent<HTMLFormElement>) => {    e.preventDefault();    const form = new FormData(e.target as HTMLFormElement);    const parsedData = Object.fromEntries(form.entries()) as unknown as TCreateProduct;    createProduct(parsedData);  }

Viewing all articles
Browse latest Browse all 11781

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>