Hello everyone probably a very simple mistake but i couldn't figure it out
Ids: [
{
key: number
}
],
this part is my definition on interface side and then i am sending the id like this
const Ids = this.selectedDocuments.map(id => ({ key: id }))
and i get this error
Type '{ key: any; }[]' is not assignable to type '[{ key: number; }]'. Target requires 1 element(s) but source may have fewer.
Thanks in advance for everyone's help.
CodePudding user response:
You have two mistakes here.
First, I assume you want your IDs to be objects with one element - a key. Then, you should do as @archon said. In addition, you can use casting, like this:
const Ids = this.selectedDocuments.map(id => ({ key: id }))
to make sure the ids are converted to numbers and this should hopefully be enough for TS to figure out what you're trying to do.
CodePudding user response:
If you want Ids
to be an array of { key: number } interface, the above definition should be const Ids: { key: number }[] = [];
instead of {[key: number]}
.
Edit: You should check the answer @liel-fridman 's as well to make sure whatever you're passing is converted to number if selectedDocuments
doesn't have a type or if it contains numeric "strings".