Skip to content

Commit 7644380

Browse files
thenicknameyyx990803
authored andcommitted
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
1 parent 3cd4af4 commit 7644380

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

types/test/options-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ Vue.component('component-with-scoped-slot', {
304304
child: {
305305
render (this: Vue, h: CreateElement) {
306306
return h('div', [
307-
this.$scopedSlots['default']({ msg: 'hi' }),
308-
this.$scopedSlots['item']({ msg: 'hello' })
307+
this.$scopedSlots['default']!({ msg: 'hi' }),
308+
this.$scopedSlots['item']!({ msg: 'hello' })
309309
])
310310
}
311311
}
@@ -314,7 +314,7 @@ Vue.component('component-with-scoped-slot', {
314314

315315
Vue.component('narrow-array-of-vnode-type', {
316316
render (h): VNode {
317-
const slot = this.$scopedSlots.default({})
317+
const slot = this.$scopedSlots.default!({})
318318
if (typeof slot !== 'string') {
319319
const first = slot[0]
320320
if (!Array.isArray(first) && typeof first !== 'string') {

types/vue.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export interface Vue {
2727
readonly $root: Vue;
2828
readonly $children: Vue[];
2929
readonly $refs: { [key: string]: Vue | Element | Vue[] | Element[] };
30-
readonly $slots: { [key: string]: VNode[] };
31-
readonly $scopedSlots: { [key: string]: ScopedSlot };
30+
readonly $slots: { [key: string]: VNode[] | undefined };
31+
readonly $scopedSlots: { [key: string]: ScopedSlot | undefined };
3232
readonly $isServer: boolean;
3333
readonly $data: Record<string, any>;
3434
readonly $props: Record<string, any>;

0 commit comments

Comments
 (0)