Set data Type in Vue data object with Typescript
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: []
};
},
})
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>()
};
},
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() : {
players: Array<any>, // if possible, replace `any` with something more specific
} {
return {
players: []
};
},
Tada! players
is now of type Array
of any
.