vuejs split to string to array


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(",");

enter image description here


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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published