Set data Type in Vue data object with Typescript Set data Type in Vue data object with Typescript vue.js vue.js

Set data Type in Vue data object with Typescript


To add to Joshua's answer, you may want to declare the type of players inline so your code doesn't get too verbose as your data gets larger.

data() {  return {    players: [] as Player[]  };},

another option:

data() {  return {    players: new Array<Player>()  };},


This should work:

declare interface Player {  cod: string,  param: string}declare interface BaseComponentData {  players: Player[]}export default Vue.extend({  name: 'basecomponent',  data(): BaseComponentData {    return {      players: []    };  },})


Your data method has an undeclared return value.

If you supply one, TypeScript will know what to expect with players.

You just need to expand the data() { line.

e.g.:

data() {  return {    players: []  };},

needs to become:

data() : {  people: Array<any>, // if possible, replace `any` with something more specific} {  return {    players: []  };},

Tada! players is now type Array of any.