Skip to content

Commit 174d851

Browse files
wxsmswatonywengveaba
authored
docs: update options-api.md (#11)
* docs: update options-api.md * docs: update * docs: update * Update src/guide/typescript/options-api.md Co-authored-by: Wang Weitao <[email protected]> * update * update * Update src/guide/typescript/options-api.md Co-authored-by: Godpu <[email protected]> Co-authored-by: Wang Weitao <[email protected]> Co-authored-by: Godpu <[email protected]> Co-authored-by: Godpu <[email protected]>
1 parent 00be59e commit 174d851

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/guide/typescript/options-api.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# TypeScript 与选项式 API {#typescript-with-options-api}
22

3-
> 这一章假设你已经阅读过了这篇 [搭配 TypeScript 使用 Vue](./overview) 的文档
3+
> 这一章假设你已经阅读了[搭配 TypeScript 使用 Vue](./overview) 的概览
44
55
:::tip
6-
虽然 Vue 的确支持在选项式 API 中使用 TypeScript,但还是推荐你搭配 TypeScript 和组合式 API 来使用 Vue,因为它提供了更简单、高效和更可靠的类型推导
6+
虽然 Vue 的确支持在选项式 API 中使用 TypeScript,但还是推荐通过 TypeScript 与组合式 API 来使用 Vue,因为它提供了更简单、更高效和更可靠的类型推导
77
:::
88

9-
## 为组件 props 标注类型 {#typing-component-props}
9+
## 为组件的 prop 标注类型 {#typing-component-props}
1010

11-
选项式 API 中对 props 的类型推导需要用 `defineComponent()` 来包裹组件。有了它,Vue 才可以通过 `props` 来推断出 props 的类型,另外还有一些其他的选项,比如 `required: true``default`
11+
选项式 API 中对 prop 的类型推导需要用 `defineComponent()` 来包裹组件。有了它,Vue 才可以通过 `props` 以及一些额外的选项,比如 `required: true``default` 来推导出 prop 的类型
1212

1313
```ts
1414
import { defineComponent } from 'vue'
1515

1616
export default defineComponent({
17-
// 启用类型推导
17+
// 启用了类型推导
1818
props: {
1919
name: String,
2020
id: [Number, String],
@@ -30,9 +30,9 @@ export default defineComponent({
3030
})
3131
```
3232

33-
然而,这种运行时 props 选项仅支持使用构造函数来作为一个 prop 的类型,而没有办法指定多层级对象或函数签名之类的复杂类型
33+
然而,这种运行时 `props` 选项仅支持使用构造函数来作为一个 prop 的类型——没有办法指定多层级对象或函数签名之类的复杂类型
3434

35-
要标记更复杂的 props 类型,我们可以使用 `PropType` 这个工具类型。
35+
我们可以使用 `PropType` 这个工具类型来标记更复杂的 prop 类型:
3636

3737
```ts
3838
import { defineComponent, PropType } from 'vue'
@@ -46,27 +46,27 @@ interface Book {
4646
export default defineComponent({
4747
props: {
4848
book: {
49-
// `Object` 提供更确定的类型
49+
// 提供相对 `Object` 更确定的类型
5050
type: Object as PropType<Book>,
5151
required: true
5252
},
53-
// 也可以标记函数类型
53+
// 也可以标记函数
5454
callback: Function as PropType<(id: number) => void>
5555
},
5656
mounted() {
5757
this.book.title // string
5858
this.book.year // number
5959

60-
// TS 错误:类型为 'string' 的参数无法
61-
// 赋值给类型为 'number' 的参数
60+
// TS Error: argument of type 'string' is not
61+
// assignable to parameter of type 'number'
6262
this.callback?.('123')
6363
}
6464
})
6565
```
6666

67-
### 约定 {#caveats}
67+
### 注意事项 {#caveats}
6868

69-
因为一个 TypeScript 的 [设计限制](https://github.com/microsoft/TypeScript/issues/38845),你在使用函数作为 prop 的 `validator``default` 选项值时需要格外小心确保使用箭头函数:
69+
因为一个 TypeScript 的 [设计限制](https://github.com/microsoft/TypeScript/issues/38845),你在使用函数作为 prop 的 `validator``default` 选项值时需要格外小心——确保使用箭头函数:
7070

7171
```ts
7272
import { defineComponent, PropType } from 'vue'
@@ -92,9 +92,9 @@ const Component = defineComponent({
9292

9393
这会防止 Typescript 将 `this` 根据函数内的环境作出不符合我们期望的类型推导。
9494

95-
## 为组件的 emits 标注类型 {#typing-component-emits}
95+
## 为组件的 emit 标注类型 {#typing-component-emits}
9696

97-
我们可以使用对象值作为 `emits` 选项的值,为所触发的事件声明期望的载荷内容类型。并且触发到所有未声明的事件时都会抛出一个类型错误
97+
我们可以为使用了对象语法作为 `emits` 选项所触发的事件声明期望的载荷内容类型。并且,所有未声明的事件调用时都会抛出一个类型错误
9898

9999
```ts
100100
import { defineComponent } from 'vue'
@@ -120,7 +120,7 @@ export default defineComponent({
120120

121121
## 为计算属性标记类型 {#typing-computed-properties}
122122

123-
一个计算属性可以根据其返回值来推导得出类型
123+
一个计算属性根据其返回值来推导其类型
124124

125125
```ts
126126
import { defineComponent } from 'vue'
@@ -142,7 +142,7 @@ export default defineComponent({
142142
})
143143
```
144144

145-
在某些场景中,你可能想要显式的标记出计算属性的类型以确保实现是正确的
145+
在某些场景中,你可能想要显式地标记出计算属性的类型以确保其实现是正确的
146146

147147
```ts
148148
import { defineComponent } from 'vue'
@@ -154,12 +154,12 @@ const Component = defineComponent({
154154
}
155155
},
156156
computed: {
157-
// 显式标注出返回值类型作为此计算属性类型
157+
// 显式标注返回类型
158158
greeting(): string {
159159
return this.message + '!'
160160
},
161161

162-
// 标注一个可写的计算属性的类型
162+
// 标注一个可写的计算属性
163163
greetingUppercased: {
164164
get(): string {
165165
return this.greeting.toUpperCase()
@@ -172,7 +172,7 @@ const Component = defineComponent({
172172
})
173173
```
174174

175-
显式的类型标注可能在某些 TypeScript 无法推导类型的循环引用的边界情况下是必须指定的
175+
在某些 TypeScript 因循环引用而无法推导类型的情况下,可能必须进行显式的类型标注
176176

177177
## 为事件处理器标注类型 {#typing-event-handlers}
178178

@@ -183,7 +183,7 @@ const Component = defineComponent({
183183
export default {
184184
methods: {
185185
handleChange(event) {
186-
// `event` 隐式定位 `any` 类型
186+
// `event` 隐式地标注为 `any` 类型
187187
console.log(event.target.value)
188188
}
189189
}
@@ -195,7 +195,7 @@ export default {
195195
</template>
196196
```
197197

198-
没有类型标注时,这个 `event` 参数会隐式定为类型 `any`。这也会在 `tsconfig.json` 中配置了 `"strict": true``"noImplicitAny": true` 时报出一个 TS 错误。因此,建议显式地为事件处理器的参数标注类型。此外,你可能需要显式地强制转换 `event` 上的属性
198+
没有类型标注时,这个 `event` 参数会隐式地标注为 `any` 类型。这也会在 `tsconfig.json` 中配置了 `"strict": true``"noImplicitAny": true` 时抛出一个 TS 错误。因此,建议显式地为事件处理器的参数标注类型。此外,你可能需要显式地强制转换 `event` 上的 property
199199

200200
```ts
201201
export default {
@@ -207,9 +207,9 @@ export default {
207207
}
208208
```
209209

210-
## 扩充全局属性 {#augmenting-global-properties}
210+
## 扩充全局 property {#augmenting-global-properties}
211211

212-
某些插件通过 [`app.config.globalProperties`](/api/application.html#app-config-globalproperties) 为所有组件都全局安装了一些属性。举个例子,我们可能为了请求数据 安装了 `this.$http`或者为了国际化翻译安装了 `this.$translate`。为了使 TypeScript 更好地支持这个行为,Vue 暴露了一个 `ComponentCustomProperties` 接口,它被设计成通过 [TypeScript 模块扩充](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) 来进行属性扩充
212+
某些插件通过 [`app.config.globalProperties`](/api/application.html#app-config-globalproperties) 为所有组件都安装了全局可用的 property。举个例子,我们可能为了请求数据而安装了 `this.$http`或者为了国际化而安装了 `this.$translate`。为了使 TypeScript 更好地支持这个行为,Vue 暴露了一个被设计为可以通过 [TypeScript 模块扩充](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation)来扩充的 `ComponentCustomProperties` 接口
213213

214214
```ts
215215
import axios from 'axios'
@@ -222,15 +222,15 @@ declare module 'vue' {
222222
}
223223
```
224224

225-
你也可以看看
225+
参考
226226

227227
- [对组件类型扩展的 TypeScript 单元测试](https://github.com/vuejs/core/blob/main/test-dts/componentTypeExtensions.test-d.tsx)
228228

229229
### 类型扩充的位置 {#type-augmentation-placement}
230230

231-
我们可以将这些类型扩充放在一个 `.ts` 文件或一个以整个项目为范围的 `*.d.ts` 文件中。无论哪一种,你都需要在 `tsconfig.json` 中将其引入。对于库或插件作者,这个文件应该在 `package.json``type` 属性中被列出
231+
我们可以将这些类型扩充放在一个 `.ts` 文件,或是一个以整个项目为范围的 `*.d.ts` 文件中。无论哪一种,确保在 `tsconfig.json` 中将其引入。对于库或插件作者,这个文件应该在 `package.json``type` property 中被列出
232232

233-
In order to take advantage of module augmentation, you will need to ensure the augmentation is placed in a [TypeScript module](https://www.typescriptlang.org/docs/handbook/modules.html). That is to say, the file needs to contain at least one top-level `import` or `export`, even if it is just `export {}`. If the augmentation is placed outside of a module, it will overwrite the original types rather than augmenting them!
233+
为了利用模块扩充的优势,你需要确保将扩充的模块放在 [TypeScript 模块](https://www.typescriptlang.org/docs/handbook/modules.html) 中。 也就是说,该文件需要包含至少一个顶级的 `import` `export`,即使它只是 `export {}`。如果扩充被放在模块之外,它将覆盖原始类型,而不是扩充!
234234

235235
## 扩充自定义选项 {#augmenting-custom-options}
236236

@@ -246,7 +246,7 @@ export default defineComponent({
246246
})
247247
```
248248

249-
没有确切的类型标注,这个钩子函数的参数会隐式定为 `any` 类型。我们可以为 `ComponentCustomOptions` 接口扩充自定义的选项来支持:
249+
如果没有确切的类型标注,这个钩子函数的参数会隐式地标注为 `any` 类型。我们可以为 `ComponentCustomOptions` 接口扩充自定义的选项来支持:
250250

251251
```ts
252252
import { Route } from 'vue-router'
@@ -258,11 +258,11 @@ declare module 'vue' {
258258
}
259259
```
260260

261-
现在这个 `beforeRouterEnter` 选项会被准确地类型化。注意这只是一个例子`vue-router` 这样类型完备的库应该在它们自己的类型定义中自动执行这些扩充
261+
现在这个 `beforeRouterEnter` 选项会被准确地类型化。注意这只是一个例子——`vue-router` 这种类型完备的库应该在它们自己的类型定义中自动执行这些扩充
262262

263-
这种类型扩充和全局属性扩充受到 [相同的限制](#type-augmentation-placement)
263+
这种类型扩充和全局 property 扩充受到[相同的限制](#type-augmentation-placement)
264264

265-
你也可以看看
265+
参考
266266

267267
- [对组件类型扩展的 TypeScript 单元测试](https://github.com/vuejs/core/blob/main/test-dts/componentTypeExtensions.test-d.tsx)
268268

0 commit comments

Comments
 (0)