@@ -4,12 +4,12 @@ import { hasOwn, isObject, isPlainObject, capitalize, hyphenate } from 'shared/u
4
4
import { observe , observerState } from '../observer/index'
5
5
import { warn } from './debug'
6
6
7
- type PropOptions = {
8
- type : Function | Array < Function > | null ,
9
- default : any ,
10
- required : ?boolean ,
11
- validator : ?Function
12
- }
7
+ // type PropOptions = {
8
+ // type: Function | Array<Function> | null,
9
+ // default: any,
10
+ // required: ?boolean,
11
+ // validator: ?Function
12
+ // }
13
13
14
14
export function validateProp (
15
15
key : string ,
@@ -23,7 +23,7 @@ export function validateProp (
23
23
const absent = ! hasOwn ( propsData , key )
24
24
let value = propsData [ key ]
25
25
// handle boolean props
26
- if ( prop . type === Boolean ) {
26
+ if ( getType ( prop . type ) === ' Boolean' ) {
27
27
if ( absent && ! hasOwn ( prop , 'default' ) ) {
28
28
value = false
29
29
} else if ( value === '' || value === hyphenate ( key ) ) {
@@ -131,31 +131,34 @@ function assertType (value: any, type: Function): {
131
131
expectedType : string
132
132
} {
133
133
let valid
134
- let expectedType
135
- if ( type === String ) {
136
- expectedType = 'string '
137
- valid = typeof value === expectedType
138
- } else if ( type === Number ) {
139
- expectedType = 'number '
140
- valid = typeof value === expectedType
141
- } else if ( type === Boolean ) {
142
- expectedType = 'boolean '
143
- valid = typeof value === expectedType
144
- } else if ( type === Function ) {
145
- expectedType = 'function '
146
- valid = typeof value === expectedType
147
- } else if ( type === Object ) {
148
- expectedType = 'Object '
134
+ let expectedType = getType ( type )
135
+ if ( expectedType === 'String' ) {
136
+ valid = typeof value === ( expectedType = 'string' )
137
+ } else if ( expectedType === 'Number' ) {
138
+ valid = typeof value === ( expectedType = 'number' )
139
+ } else if ( expectedType === 'Boolean' ) {
140
+ valid = typeof value === ( expectedType = 'boolean' )
141
+ } else if ( expectedType === 'Function' ) {
142
+ valid = typeof value === ( expectedType = 'function' )
143
+ } else if ( expectedType === 'Object' ) {
149
144
valid = isPlainObject ( value )
150
- } else if ( type === Array ) {
151
- expectedType = 'Array '
145
+ } else if ( expectedType === 'Array' ) {
152
146
valid = Array . isArray ( value )
153
147
} else {
154
- expectedType = type . name || type . toString ( )
155
148
valid = value instanceof type
156
149
}
157
150
return {
158
151
valid ,
159
152
expectedType
160
153
}
161
154
}
155
+
156
+ /**
157
+ * Use function string name to check built-in types,
158
+ * because a simple equality check will fail when running
159
+ * across different vms / iframes.
160
+ */
161
+ function getType ( fn ) {
162
+ const match = fn && fn . toString ( ) . match ( / ^ f u n c t i o n ( \w + ) / )
163
+ return match && match [ 1 ]
164
+ }
0 commit comments