Skip to content

Commit af208d9

Browse files
committed
ts reference links for options api
1 parent 92f1b4b commit af208d9

File tree

6 files changed

+109
-9
lines changed

6 files changed

+109
-9
lines changed

src/guide/components/events.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ const emit = defineEmits(['inFocus', 'submit'])
102102

103103
The returned `emit` function can be used to emit events in JavaScript.
104104

105-
See also: [Typing Component Emits](/guide/typescript/composition-api.html#typing-component-emits) <Badge type="ts" text="TS" />
106-
107105
If not using `<script setup>`, events should be declared using the [`emits`](/api/options-state.html#emits) option, and the `emit` function is exposed on the `setup()` context:
108106

109107
```js
@@ -126,6 +124,41 @@ export default {
126124

127125
</div>
128126

127+
The `emits` option also supports an object syntax, which allows us to perform runtime validation of the payload of the emitted events:
128+
129+
<div class="composition-api">
130+
131+
```vue
132+
<script setup>
133+
const emit = defineEmits({
134+
submit(payload) {
135+
// return `true` or `false` to indicate
136+
// validation pass / fail
137+
}
138+
})
139+
</script>
140+
```
141+
142+
See also: [Typing Component Emits](/guide/typescript/composition-api.html#typing-component-emits) <Badge type="ts" text="TS" />
143+
144+
</div>
145+
<div class="options-api">
146+
147+
```js
148+
export default {
149+
emits: {
150+
submit(payload) {
151+
// return `true` or `false` to indicate
152+
// validation pass / fail
153+
}
154+
}
155+
}
156+
```
157+
158+
See also: [Typing Component Emits](/guide/typescript/options-api.html#typing-component-emits) <Badge type="ts" text="TS" />
159+
160+
</div>
161+
129162
Although optional, it is recommended to define all emitted events in order to better document how a component should work. It also allows Vue to exclude known listeners from [fallthrough attributes](/guide/components/attrs.html#v-on-listener-inheritance).
130163

131164
:::tip

src/guide/components/props.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ aside: deep
1010

1111
Vue components require explicit props declaration so that Vue knows what external props passed to the component should be treated as fallthrough attributes (which will be discussed in the next section).
1212

13+
<div class="composition-api">
14+
1315
In SFCs using `<script setup>`, props can be declared using the `defineProps()` macro:
1416

1517
```vue
@@ -20,9 +22,7 @@ console.log(props.foo)
2022
</script>
2123
```
2224

23-
In non-`<script setup>` components, props are declared using the `props` option:
24-
25-
<div class="composition-api">
25+
In non-`<script setup>` components, props are declared using the [`props`](/api/options-state.html#props) option:
2626

2727
```js
2828
export default {
@@ -34,9 +34,14 @@ export default {
3434
}
3535
```
3636

37+
Notice the argument passed to `defineProps()` is the same as the value provided to the `props` options: the same props options API is shared between the two declaration styles.
38+
3739
</div>
40+
3841
<div class="options-api">
3942

43+
Props are declared using the [`props`](/api/options-state.html#props) option:
44+
4045
```js
4146
export default {
4247
props: ['foo'],
@@ -49,10 +54,22 @@ export default {
4954

5055
</div>
5156

52-
Notice the argument passed to `defineProps()` is the same as the value provided to the `props` options: the same props options API is shared between the two declaration styles.
53-
5457
In addition to declaring props using an array of strings, we can also use the object syntax:
5558

59+
<div class="options-api">
60+
61+
```js
62+
export default {
63+
props: {
64+
title: String,
65+
likes: Number
66+
}
67+
}
68+
```
69+
70+
</div>
71+
<div class="composition-api">
72+
5673
```js
5774
// in <script setup>
5875
defineProps({
@@ -62,7 +79,7 @@ defineProps({
6279
```
6380

6481
```js
65-
// non-<script setup>
82+
// in non-<script setup>
6683
export default {
6784
props: {
6885
title: String,
@@ -71,10 +88,18 @@ export default {
7188
}
7289
```
7390

91+
</div>
92+
7493
For each property in the object declaration syntax, the key is the name of the prop, while the value should be the constructor function of the expected type.
7594

7695
This not only documents your component, but will also warn other developers using your component in the browser console if they pass the wrong type. We will discuss more details about [prop validation](#prop-validation) further down this page.
7796

97+
<div class="options-api">
98+
99+
See also: [Typing Component Props](/guide/typescript/options-api.html#typing-component-props) <Badge type="ts" text="TS" />
100+
101+
</div>
102+
78103
<div class="composition-api">
79104

80105
If you are using TypeScript with `<script setup>`, it's also possible to declare props using pure type annotations:

src/guide/essentials/computed.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Try to change the value of the `books` array in the application `data` and you w
8989

9090
You can data-bind to computed properties in templates just like a normal property. Vue is aware that `this.publishedBooksMessage` depends on `this.author.books`, so it will update any bindings that depend on `this.publishedBooksMessage` when `this.author.books` changes.
9191

92+
See also: [Typing Computed Properties](/guide/typescript/options-api.html#typing-computed) <Badge type="ts" text="TS" />
93+
9294
</div>
9395

9496
<div class="composition-api">

src/guide/essentials/event-handling.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ A method handler automatically receives the native DOM Event object that trigger
118118

119119
See also: [Typing Event Handlers](/guide/typescript/composition-api.html#typing-event-handlers) <Badge type="ts" text="TS" />
120120

121+
</div>
122+
<div class="options-api">
123+
124+
See also: [Typing Event Handlers](/guide/typescript/options-api.html#typing-event-handlers) <Badge type="ts" text="TS" />
125+
121126
</div>
122127

123128
### Method vs. Inline Detection

src/guide/reusability/plugins.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Our `$translate` function will take a string such as `greetings.hello`, look ins
8585
<h1>{{ $translate('greetings.hello') }}</h1>
8686
```
8787

88+
See also: [Augmenting Global Properties](/guide/typescript/options-api.html#augmenting-global-properties) <Badge type="ts" text="TS" />
89+
8890
:::tip
8991
Use global properties scarcely, since it can quickly become confusing if too many global properties injected by different plugins are used throughout an app.
9092
:::

src/guide/typescript/options-api.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ While Vue does support TypeScript usage with Options API, it is recommended to u
88

99
## Typing Component Props
1010

11-
When `defineComponent()` is used, Vue is able to infer the types for the props based on the `props` option, taking additional options such as `required: true` and `default` into account:
11+
Type inference for props in Options API requires wrapping the component with `defineComponent()`. With it, Vue is able to infer the types for the props based on the `props` option, taking additional options such as `required: true` and `default` into account:
1212

1313
```ts
1414
import { defineComponent } from 'vue'
@@ -174,6 +174,39 @@ const Component = defineComponent({
174174

175175
Explicit annotations may also be required in some edge cases where TypeScript fails to infer the type of a computed property due to circular inference loops.
176176

177+
## Typing Event Handlers
178+
179+
When dealing with native DOM events, it might be useful to type the argument we pass to the handler correctly. Let's take a look at this example:
180+
181+
```vue
182+
<script lang="ts">
183+
export default {
184+
methods: {
185+
handleChange(event) {
186+
// `event` implicitly has `any` type
187+
console.log(event.target.value)
188+
}
189+
}
190+
}
191+
</script>
192+
193+
<template>
194+
<input type="text" @change="handleChange" />
195+
</template>
196+
```
197+
198+
Without type annotation, the `event` argument will implicitly have a type of `any`. This will also result in a TS error if `"strict": true` or `"noImplicitAny": true` are used in `tsconfig.json`. It is therefore recommended to explicitly annotate the argument of event handlers. In addition, you may need to explicitly cast properties on `event`:
199+
200+
```ts
201+
export default {
202+
methods: {
203+
handleChange(event: Event) {
204+
console.log((event.target as HTMLInputElement).value)
205+
}
206+
}
207+
}
208+
```
209+
177210
## Augmenting Global Properties
178211

179212
Some plugins install globally available properties to all component instances via [`app.config.globalProperties`](/api/application.html#app-config-globalproperties). For example, we may install `this.$http` for data-fetching or `this.$translate` for internationalization. To make this play well with TypeScript, Vue exposes a `ComponentCustomProperties` interface designed to be augmented via [TypeScript module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation):

0 commit comments

Comments
 (0)