Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-core): add ComponentCustomProperties interface #982

Merged
merged 5 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ import {
} from './componentRenderUtils'
import { warn } from './warning'

//
/**
* Custom properties added to component instances in any way and can be accessed through `this`
*
* @example
* Here is an example of adding a property `$router` to every component instance:
* ```ts
* import { createApp } from 'vue'
* import { Router, createRouter } from 'vue-router'
*
* declare module '@vue/runtime-core' {
* interface ComponentCustomProperties {
* $router: Router
* }
* }
*
* // effectively adding the router to every component instance
* const app = createApp({})
* const router = createRouter()
* app.config.globalProperties.$router = router
*
* const vm = app.mount('#app')
* // we cann access the router from the instance
posva marked this conversation as resolved.
Show resolved Hide resolved
* vm.$router.push('/')
* ```
*/
export interface ComponentCustomProperties {}

// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
Expand Down Expand Up @@ -53,7 +81,8 @@ export type ComponentPublicInstance<
UnwrapRef<B> &
D &
ExtractComputedReturns<C> &
M
M &
ComponentCustomProperties

const publicPropertiesMap: Record<
string,
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ export {
ComponentOptionsWithObjectProps as ComponentOptionsWithProps,
ComponentOptionsWithArrayProps
} from './componentOptions'
export { ComponentPublicInstance } from './componentProxy'
export {
ComponentPublicInstance,
ComponentCustomProperties
} from './componentProxy'
export {
Renderer,
RendererNode,
Expand Down
20 changes: 20 additions & 0 deletions test-dts/componentCustomProperties.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectError } from 'tsd'
import { defineComponent } from './index'

declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
state: 'stopped' | 'running'
}
}

export const Custom = defineComponent({
data: () => ({ counter: 0 }),
methods: {
aMethod() {
expectError(this.notExisting)
this.counter++
this.state = 'running'
expectError((this.state = 'not valid'))
}
}
})