3.0.0-beta.18
https://github.com/whu-luojian/webpack-vue3
<script lang="ts">
import { defineComponent, ref } from 'vue'
type TodoItem = {
id: number
text: string
completed: boolean
}
type Props = {
todo: TodoItem
}
export default defineComponent({
name: 'TodoItem',
props: {
todo: {
type: Object,
required: true
}
},
setup(props: Props) {
console.log(JSON.stringify(props, null, 2))
const count = ref(0)
return {
count
}
}
})
</script>
it will work correctly or there are any better solution to properly type props in TS
setup function throw a ts error by Vetur:
No overload matches this call.
The last overload gave the following error.
Type '(this: void, props: Props) => { count: Ref
Types of parameters 'props' and 'props' are incompatible.
Type 'Readonly<{ todo: { [key: string]: any; }; } & {}>' is not assignable to type 'Props'.
Types of property 'todo' are incompatible.
Type '{ [key: string]: any; }' is missing the following properties from type 'TodoItem': id, text, completedVetur(2769)
Related issues:
https://github.com/vuejs/vue-next/issues/1155
https://github.com/vuejs/vue-next/issues/1114
Just use the PropType, and remove type annotation on setup(props: Props):
export default defineComponent({
name: 'TodoItem',
props: {
todo: {
type: Object as PropType<TodoItem>,
required: true
}
},
setup(props) {
console.log(JSON.stringify(props, null, 2))
const count = ref(0)
return {
count
}
}
})
Most helpful comment
Just use the
PropType, and remove type annotation onsetup(props: Props):