From 73c6bea5269f96dac1c955accb482fcd492a4aca Mon Sep 17 00:00:00 2001 From: thenickname Date: Sat, 1 Dec 2018 05:31:45 +0100 Subject: [PATCH] Typings: Improve $slots and $scopedSlots type to prevent unchecked access to undefined (#8946) * fix(types): Declare $scopedSlots as potentially undefined to enable stricter TS checks * fix(types): Fix tests * fix(types): declare $slots option as potentially undefined declare $slots option as potentially undefined to enable stricter TS checks --- types/test/options-test.ts | 6 +++--- types/vue.d.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/types/test/options-test.ts b/types/test/options-test.ts index 8755564c2a3..6c817e23d19 100644 --- a/types/test/options-test.ts +++ b/types/test/options-test.ts @@ -304,8 +304,8 @@ Vue.component('component-with-scoped-slot', { child: { render (this: Vue, h: CreateElement) { return h('div', [ - this.$scopedSlots['default']({ msg: 'hi' }), - this.$scopedSlots['item']({ msg: 'hello' }) + this.$scopedSlots['default']!({ msg: 'hi' }), + this.$scopedSlots['item']!({ msg: 'hello' }) ]) } } @@ -314,7 +314,7 @@ Vue.component('component-with-scoped-slot', { Vue.component('narrow-array-of-vnode-type', { render (h): VNode { - const slot = this.$scopedSlots.default({}) + const slot = this.$scopedSlots.default!({}) if (typeof slot !== 'string') { const first = slot[0] if (!Array.isArray(first) && typeof first !== 'string') { diff --git a/types/vue.d.ts b/types/vue.d.ts index 3832f2c9e41..ba8e68e942e 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -27,8 +27,8 @@ export interface Vue { readonly $root: Vue; readonly $children: Vue[]; readonly $refs: { [key: string]: Vue | Element | Vue[] | Element[] }; - readonly $slots: { [key: string]: VNode[] }; - readonly $scopedSlots: { [key: string]: ScopedSlot }; + readonly $slots: { [key: string]: VNode[] | undefined }; + readonly $scopedSlots: { [key: string]: ScopedSlot | undefined }; readonly $isServer: boolean; readonly $data: Record; readonly $props: Record;