types: use stricter defaults for component data and props

This commit is contained in:
Evan You
2018-10-04 17:33:20 -04:00
parent 05b70f790c
commit 511ac0bd2f
5 changed files with 29 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import { ChildrenFlags } from '../flags'
import { createComponentVNode, Slots } from '../vdom'
import { Component, ComponentType, ComponentClass } from '../component'
import { unwrap } from '@vue/observer'
export interface AsyncComponentFactory {
(): Promise<ComponentType>
@@ -92,7 +93,7 @@ export function createAsyncComponent(
} else if (this.comp) {
return createComponentVNode(
this.comp,
props,
unwrap(props),
slots,
ChildrenFlags.STABLE_SLOTS
)

View File

@@ -4,15 +4,23 @@ import { Slots } from '../vdom'
const contextStore = observable() as Record<string | symbol, any>
export class Provide extends Component {
interface ProviderProps {
id: string | symbol
value: any
}
export class Provide extends Component<{}, ProviderProps> {
updateValue() {
contextStore[this.$props.id] = this.$props.value
// TS doesn't allow symbol as index :/
// https://github.com/Microsoft/TypeScript/issues/24587
contextStore[this.$props.id as string] = this.$props.value
}
created() {
if (__DEV__) {
if (contextStore.hasOwnProperty(this.$props.id)) {
const { id } = this.$props
if (contextStore.hasOwnProperty(id)) {
console.warn(
`A context provider with id ${this.$props.id} already exists.`
`A context provider with id ${id.toString()} already exists.`
)
}
this.$watch(
@@ -31,7 +39,7 @@ export class Provide extends Component {
beforeUpdate() {
this.updateValue()
}
render(props: any, slots: Slots) {
render(props: ProviderProps, slots: Slots) {
return slots.default && slots.default()
}
}