using defineComponent when using Typescript with Vue #10302
-
I want to start only using Typescript for any future projects. According to all the sources i found while researching, everybody was using I could not identify WHY this is necessary. Can someone elaborate? Especially in regards to using
and did not encouter one single problem. So why the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The quick explanation: With 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>
}
}) |
Beta Was this translation helpful? Give feedback.
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: