I decided to try out and learn Typescript in my React App and I got into first error I can't figure out or find the answer for it.
Is it possible to access only one specific key in exported type and change its value?
So I am exporting my type like this:
export type MyFirstTestType = {
id: number;
name: string;
price: number;
object1: {
name: string;
price: number;
},
object2: {
name: string;
price: number;
},
And then I am getting the values from my external API
const getTestItems = async (): Promise<MyFirstTestType[]> =>
await (await fetch('apiEndpoint')).json();
In classic JS I would just create new state for price
const [itemPrice, setItemPrice] = React.useState(data.price)
and then easily update state with onClick function. Is is possible to achieve something like this with TypeScript? So I can update only the price from MyFirstTestType?
I tried similar thing:
const [price, setPrice] = React.useState<MyFirstTestType>({price: 100});
But this throw error of -> Type '{ price: number; }' is missing the rest of the properties in MyFirstTestType (id, name,...)