With the following code, Vetur inferenced x,y
to have type number | undefined
.
The undefined
cause tons of warning on further usage of x,y
.
How can I remove the undefined
from the type inference?
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
props: { x: Number, y: Number },
setup(props) {
let { x, y } = props
},
})
</script>
CodePudding user response:
All props
are optional (as in it's possibly undefined
) by default, so your prop
s are typed as number | undefined
.
You can declare the prop
s as required using the long form of prop declaration, which removes | undefined
from the prop
's type:
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
props: {
x: {
type: Number,
required: true, //