Skip to content

Commit adbf45d

Browse files
alexzhang1030sxzz
andauthored
docs: add chain-call (#393)
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
1 parent 8e82451 commit adbf45d

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

docs/.vitepress/locales/common.ts

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ export const sidebar = (lang: string): DefaultTheme.SidebarItem[] => {
123123
text: 'setupSFC',
124124
link: `${urlPrefix}/macros/setup-sfc`,
125125
},
126+
{
127+
text: 'chainCall',
128+
link: `${urlPrefix}/macros/chain-call`,
129+
},
126130
],
127131
},
128132
],

docs/features/export-props.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| Features | Supported |
88
| :----------: | :----------------: |
99
| Vue 3 | :white_check_mark: |
10-
| Nuxt 3 | ? |
11-
| Vue 2 | ? |
10+
| Nuxt 3 | :question: |
11+
| Vue 2 | :question: |
1212
| Volar Plugin | :white_check_mark: |
1313

1414
## Usage

docs/macros/chain-call.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# chainCall
2+
3+
<StabilityLevel level="experimental" />
4+
5+
Extends `defineProps`, support call `withDefaults` as a chain.
6+
7+
| Features | Supported |
8+
| :----------: | :----------------: |
9+
| Vue 3 | :white_check_mark: |
10+
| Nuxt 3 | :question: |
11+
| Vue 2 | :question: |
12+
| TypeScript | :white_check_mark: |
13+
| Volar Plugin | :x: |
14+
15+
::: tip
16+
17+
- `chainCall` does not support `definePropsRefs`
18+
- To fully support TypeScript, you need to import this macro from `unplugin-vue-macros/macros`.
19+
20+
:::
21+
22+
## Basic Usage
23+
24+
```vue
25+
<script setup lang="ts">
26+
const props = defineProps<{
27+
foo?: string
28+
bar?: number[]
29+
baz?: boolean
30+
}>().withDefaults({
31+
foo: '111',
32+
bar: () => [1, 2, 3],
33+
})
34+
</script>
35+
```
36+
37+
::: details Compiled Code
38+
39+
```vue
40+
<script setup lang="ts">
41+
const props = withDefaults(
42+
defineProps<{
43+
foo?: string
44+
bar?: number[]
45+
baz?: boolean
46+
}>(),
47+
{
48+
foo: '111',
49+
bar: () => [1, 2, 3],
50+
}
51+
)
52+
</script>
53+
```
54+
55+
:::
56+
57+
Also support [props destructuring](/features/reactivity-transform.html) and JSX:
58+
59+
```vue
60+
<script setup lang="tsx">
61+
const { foo } = defineProps<{ foo: string }>().withDefaults({
62+
foo: '111',
63+
})
64+
</script>
65+
```
66+
67+
## TypeScript
68+
69+
To fully support TypeScript, you need to import this macro from `unplugin-vue-macros/macros` with specific syntax.
70+
71+
```vue
72+
<script setup lang="ts">
73+
import { defineProps } from 'unplugin-vue-macros/macros' assert { type: 'macro' }
74+
75+
defineProps<{
76+
/* ... */
77+
}>().withDefaults({
78+
/* ... */
79+
})
80+
// ✅ type safe
81+
</script>
82+
```
83+
84+
Works without import assertion, but tsc will report an error:
85+
86+
```ts
87+
defineProps<{
88+
/* ... */
89+
}>().withDefaults({
90+
/* ... */
91+
})
92+
// ❌ Property 'withDefaults' does not exist on type 'DefineProps<{ /* ... */ }>'.
93+
```

docs/macros/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Please make sure `unplugin-vue-macros` is set up correctly. If you haven't yet,
2424
- [defineEmit](/macros/define-emit)
2525
- [setupComponent](/macros/setup-component)
2626
- [setupSFC](/macros/setup-sfc)
27+
- [chainCall](/macros/chain-call)

docs/zh-CN/macros/chain-call.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# chainCall
2+
3+
<StabilityLevel level="experimental" />
4+
5+
扩展 `defineProps`,支持链式调用 `withDefaults`
6+
7+
| Features | Supported |
8+
| :----------: | :----------------: |
9+
| Vue 3 | :white_check_mark: |
10+
| Nuxt 3 | :question: |
11+
| Vue 2 | :question: |
12+
| TypeScript | :white_check_mark: |
13+
| Volar Plugin | :x: |
14+
15+
::: tip
16+
17+
- `chainCall` 不支持 `definePropsRefs`
18+
- 你需要从 `unplugin-vue-macros/macros` 中导入此宏以获取更好的类型检查。
19+
20+
:::
21+
22+
## 基本用法
23+
24+
```vue
25+
<script setup lang="ts">
26+
const props = defineProps<{
27+
foo?: string
28+
bar?: number[]
29+
baz?: boolean
30+
}>().withDefaults({
31+
foo: '111',
32+
bar: () => [1, 2, 3],
33+
})
34+
</script>
35+
```
36+
37+
::: details 编译后的代码
38+
39+
```vue
40+
<script setup lang="ts">
41+
const props = withDefaults(
42+
defineProps<{
43+
foo?: string
44+
bar?: number[]
45+
baz?: boolean
46+
}>(),
47+
{
48+
foo: '111',
49+
bar: () => [1, 2, 3],
50+
}
51+
)
52+
</script>
53+
```
54+
55+
:::
56+
57+
也支持 [props 解构](/features/reactivity-transform.html) 和 JSX:
58+
59+
```vue
60+
<script setup lang="tsx">
61+
const { foo } = defineProps<{ foo: string }>().withDefaults({
62+
foo: '111',
63+
})
64+
</script>
65+
```
66+
67+
## TypeScript
68+
69+
为了更好的类型支持,你需要使用特定的语法从 `unplugin-vue-macros/macros` 中导入此宏。
70+
71+
```vue
72+
<script setup lang="ts">
73+
import { defineProps } from 'unplugin-vue-macros/macros' assert { type: 'macro' }
74+
75+
defineProps<{
76+
/* ... */
77+
}>().withDefaults({
78+
/* ... */
79+
})
80+
// ✅ 类型安全
81+
</script>
82+
```
83+
84+
没有 `import` 也可以正常使用,但 `tsc` 会报告一个类型错误:
85+
86+
```ts
87+
defineProps<{
88+
/* ... */
89+
}>().withDefaults({
90+
/* ... */
91+
})
92+
// ❌ Property 'withDefaults' does not exist on type 'DefineProps<{ /* ... */ }>'.
93+
```

docs/zh-CN/macros/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
- [defineEmit](/zh-CN/macros/define-emit)
2525
- [setupComponent](/zh-CN/macros/setup-component)
2626
- [setupSFC](/zh-CN/macros/setup-sfc)
27+
- [chainCall](/macros/chain-call)

0 commit comments

Comments
 (0)