Skip to content

Commit a679e81

Browse files
Document PropType usage with the Composition API (#2157)
1 parent e39b5d1 commit a679e81

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/guide/typescript/composition-api.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,49 @@ export default defineComponent({
131131
})
132132
```
133133

134+
### Complex prop types
135+
136+
With type-based declaration, a prop can use a complex type much like any other type:
137+
138+
```vue
139+
<script setup lang="ts">
140+
interface Book {
141+
title: string
142+
author: string
143+
year: number
144+
}
145+
146+
const props = defineProps<{
147+
book: Book
148+
}>()
149+
</script>
150+
```
151+
152+
For runtime declaration, we can use the `PropType` utility type:
153+
154+
```ts
155+
import type { PropType } from 'vue'
156+
157+
const props = defineProps({
158+
book: Object as PropType<Book>
159+
})
160+
```
161+
162+
This works in much the same way if we're specifying the `props` option directly:
163+
164+
```ts
165+
import { defineComponent } from 'vue'
166+
import type { PropType } from 'vue'
167+
168+
export default defineComponent({
169+
props: {
170+
book: Object as PropType<Book>
171+
}
172+
})
173+
```
174+
175+
The `props` option is more commonly used with the Options API, so you'll find more detailed examples in the guide to [TypeScript with Options API](/guide/typescript/options-api.html#typing-component-props). The techniques shown in those examples also apply to runtime declarations using `defineProps()`.
176+
134177
## Typing Component Emits {#typing-component-emits}
135178

136179
In `<script setup>`, the `emit` function can also be typed using either runtime declaration OR type declaration:

0 commit comments

Comments
 (0)