Using TypeScript version ^3.5.3
Consider the following JSON data:
const config = {
id: 1,
title: "A good day",
body: "Very detailed stories"
publishedAt: "2021-01-20 12:21:12"
}
To update the title using spread syntax:
const newConfig = {
...config,
title: "A new day"
}
The updated newConfig
object will be:
{
id: 1,
title: "A new day",
body: "Very detailed stories"
publishedAt: "2021-01-20 12:21:12"
}
In a different scenario, we have:
const config = {
id: 1,
articleConfig: {
version: "2",
configs: [
{
title: "A good day",
body: "Very detailed stories"
publishedAt: "2021-01-20 12:21:12"
}
]
}
}
An attempt to change the title
value like this:
const newConfig = {
...config,
articleConfig: {
configs: [
{
title: "A new day"
}
]
}
}
Results in breaking the defined JSON schema:
const newConfig: {
id: number;
articleConfig: {
version: string;
configs: {
title: string;
body: string;
publishedAt: string;
}[];
};
}
Is there a simple way to overwrite just one item in such type of JSON structure?