Skip to content

Commit

Permalink
Typings: Improve $slots and $scopedSlots type to prevent unchecked ac…
Browse files Browse the repository at this point in the history
…cess 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
  • Loading branch information
thenickname authored and yyx990803 committed Dec 1, 2018
1 parent 3cd4af4 commit 7644380
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions types/test/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
])
}
}
Expand All @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
readonly $props: Record<string, any>;
Expand Down

0 comments on commit 7644380

Please sign in to comment.