Categories
Vue Answers

How to set data type in Vue.js data object with TypeScript?

Spread the love

Sometimes, we want to set data type in Vue.js data object with TypeScript.

In this article, we’ll look at how to set data type in Vue.js data object with TypeScript.

How to set data type in Vue.js data object with TypeScript?

To set data type in Vue.js data object with TypeScript, we can set the type using as.

For instance, we write

<template>
  <div ref="root">This is a root element</div>
</template>

<script lang="ts">
export default {
  //...
  data() {
    return {
      players: [] as Player[],
    };
  },
  //...
};
</script>

to set the players reactive property to the Player[] type with as Player[].

Conclusion

To set data type in Vue.js data object with TypeScript, we can set the type using as.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.