I write code in vsocde with vue3 and typescript.
If you try to move the value from obj to itemObj after specifying the type with the following code, you get an error:
typeObj={
Timeline: {
Items—ItemsObj[];
};
};
typeItemsObj={
key:string —{
CharacterName:string;
};
};
// Abbreviated
constobj=ref<Obj>();
constructsObj=ref<ItemsObj>();;
// Abbreviated
itemsObj.value=obj.value?.Timeline?.Items;
The following error will be printed when the vscode is itemsObj.value
.
You cannot assign type 'itemsObj[]|undefined' to type 'itemsObj|undefined'.
It is listed as itemsObj[] in type obj, so it is arrayed.
[key:string]:{
and array are accepted among type itemObj.
I think the method of setting type itemObj is wrong, but
I don't know how to write correctly.
I would appreciate it if you could let me know what should be corrected.
Thank you for your cooperation.
The following corrections are no longer problematic
typeObj={
Timeline: {
Items—ItemsObj[];
};
};
typeItemObj={
CharacterName:string;//1) Delete [key:string]
};
// Abbreviated
constobj=ref<Obj>();
Arranging & Initial Value Empty Array within constructsObj=ref<ItemObj[]>([]);//2)>
// Abbreviated
itemsObj.value=obj.value.Timeline.Items;//3) ?Delete
I set it as a key for an array, but the original data was not an associative array, so I deleted it
Specifies the inside of the generic as an array.If we don't do this, we'll have a 1:1 relationship.Without further initialization, the typescript determines that it may be undefined.
Similarly, the variable is followed by ?.This means that it is considered undefined
The above modifications allow you to match (or recognize) the type and eliminate errors.
If you take them one by one, they say, "That's right," but if there's a mistake at the same time, you'll be overwhelmed.
Now that I have another experience, I don't think I'll make the same mistake (remember it properly)
© 2024 OneMinuteCode. All rights reserved.