You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Add type definition for globalProperties in TypeScript chapter. (vuejs#723)
* Add type definition for `globalProperties`.
* Add type definition for `globalProperties`.
* Fix some grammatical errors and code errors, and optimize the content.
* Further optimized document content expression.
* Modified some codes and added descriptions about TypeScript modules.
* Update src/guide/typescript-support.md
Co-authored-by: Daniel Roe <[email protected]>
Co-authored-by: skirtle <[email protected]>
Co-authored-by: Daniel Roe <[email protected]>
Vue 3 provides a [`globalProperties` object](../api/application-config.html#globalproperties) that can be used to add a global property that can be accessed in any component instance. For example, a [plugin](./plugins.html#writing-a-plugin) might want to inject a shared global object or function.
In order to tell TypeScript about these new properties, we can use [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation).
178
+
179
+
In the above example, we could add the following type declaration:
180
+
181
+
```ts
182
+
importaxiosfrom'axios'
183
+
184
+
declaremodule'@vue/runtime-core' {
185
+
exportinterfaceComponentCustomProperties {
186
+
$http:typeofaxios
187
+
$validate: (data:object, rule:object) =>boolean
188
+
}
189
+
}
190
+
```
191
+
192
+
We can put this type declaration in the same file, or in a project-wide `*.d.ts` file (for example, in the `src/typings` folder that is automatically loaded by TypeScript). For library/plugin authors, this file should be specified in the `types` property in `package.json`.
193
+
194
+
::: warning Make sure the declaration file is a TypeScript module
195
+
In order to take advantage of module augmentation, you will need to ensure there is at least one top-level `import` or `export` in your file, even if it is just `export {}`.
196
+
197
+
[In TypeScript](https://www.typescriptlang.org/docs/handbook/modules.html), any file containing a top-level `import` or `export` is considered a 'module'. If type declaration is made outside of a module, it will overwrite the original types rather than augmenting them.
198
+
:::
199
+
200
+
For more information about the `ComponentCustomProperties` type, see its [definition in `@vue/runtime-core`](https://github.com/vuejs/vue-next/blob/2587f36fe311359e2e34f40e8e47d2eebfab7f42/packages/runtime-core/src/componentOptions.ts#L64-L80) and [the TypeScript unit tests](https://github.com/vuejs/vue-next/blob/master/test-dts/componentTypeExtensions.test-d.tsx) to learn more.
201
+
156
202
### Annotating Return Types
157
203
158
204
Because of the circular nature of Vue’s declaration files, TypeScript may have difficulties inferring the types of computed. For this reason, you may need to annotate the return type of computed properties.
0 commit comments