Issue
I've got a string that's formatted in a textbox as comma-separated, however when I try and split it convert it to an array to loop through them I'm getting the error
Type 'string[]' is not assignable to type 'string'
My textbox (using ionic, but essentially just a textarea when rendered
<ion-textarea type="text" v-model="CSVItems" placeholder="e.g. chicken, rice, peas"></ion-textarea>
The data/methods are as follows (cut down to just necessary for the post)
data() {
return {
CSVItems: "",
myResult: "",
};
},
methods: {
addItem: function() {
this.myResult = this.CSVItems.split(",");
},
Solution
The error is about trying to store an array of strings to a property declared as a string. To inform tslint about the correct type, instantiate myResult as an array of strings:
data: () => ({
CSVItems: "",
myResult: [] as string[]
})
Additionally, you might want to also trim before assigning:
this.myResult = this.CSVItems.split(',').map(s => s.trim());
Answered By - tao

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.