From 52a59790a5b6e16c9f8cc737fcd784413dda282d Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 15 Jul 2022 16:52:27 +0800 Subject: [PATCH] fix(types): fix $children and $root instance types fix #12655 --- types/test/v3/define-component-test.tsx | 1 + types/test/vue-test.ts | 1 + types/vue.d.ts | 10 ++++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/types/test/v3/define-component-test.tsx b/types/test/v3/define-component-test.tsx index 2d6fca73d..fde804c40 100644 --- a/types/test/v3/define-component-test.tsx +++ b/types/test/v3/define-component-test.tsx @@ -1148,6 +1148,7 @@ defineComponent({ this.$on('foo', () => {}) this.$ssrContext this.$isServer + this.$children[0].$root.$children return h('div', {}, [...this.$slots.default!]) } }) diff --git a/types/test/vue-test.ts b/types/test/vue-test.ts index 45e7bc7a7..8489f3ae8 100644 --- a/types/test/vue-test.ts +++ b/types/test/vue-test.ts @@ -16,6 +16,7 @@ class Test extends Vue { this.$isServer this.$ssrContext this.$vnode + this.$root.$children[0].$children[0] } // test property reification diff --git a/types/vue.d.ts b/types/vue.d.ts index b57c5016d..a86e2eebf 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -35,6 +35,8 @@ export interface CreateElement { ): VNode } +type NeverFallback = [T] extends [never] ? D : T + export interface Vue< Data = Record, Props = Record, @@ -47,10 +49,10 @@ export interface Vue< // properties with different types in defineComponent() readonly $data: Data readonly $props: Props - readonly $parent: Parent extends never ? Vue : Parent - readonly $root: Root extends never ? Vue : Root - readonly $children: Children extends never ? Vue[] : Children - readonly $options: ComponentOptions + readonly $parent: NeverFallback + readonly $root: NeverFallback + readonly $children: NeverFallback + readonly $options: NeverFallback> $emit: Emit // Vue 2 only or shared