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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *