Skip to content

using defineComponent when using Typescript with Vue #10302

Discussion options

You must be logged in to vote

The quick explanation: defineComponent() predates <script setup>, so before we had that, defineComponent() was essentially a no-op function whose only purpose was to allow for proper type inference in Typescript.

With <script setup>, that is no longer needed in SFCs. However, if you want to write a component with TSX, you would still use it:

const component = defineComponent({
  props: {
    msg: String
  },
  setup(props) {
   // defineComponent allows TS to infer the proper interface for the `props` argument from the props options
    return () => <div>{ props.msg }</div>
  }
})

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@DevKame
Comment options

@LinusBorg
Comment options

@DevKame
Comment options

Answer selected by DevKame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants