From 4bcf4b020bd65f06abd863b57ebdb62df48095d6 Mon Sep 17 00:00:00 2001 From: kikuchan Date: Sun, 12 Sep 2021 20:36:48 +0900 Subject: [PATCH 1/2] test(ssr-string.spec.js): should not warn for Date prop when it comes from another context --- test/ssr/ssr-string.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/ssr/ssr-string.spec.js b/test/ssr/ssr-string.spec.js index 33094be9c33..4a537d997d6 100644 --- a/test/ssr/ssr-string.spec.js +++ b/test/ssr/ssr-string.spec.js @@ -1666,6 +1666,20 @@ describe('SSR: renderToString', () => { done() }) }) + + it('should not warn for Date prop when it comes from another context', done => { + const date = VM.runInNewContext('new Date()') + new Vue({ + props: { + date: Date, + }, + propsData: { + date + } + }) + expect('Invalid prop: type check failed for prop "date". Expected Date, got Date').not.toHaveBeenWarned() + done() + }) }) function renderVmWithOptions (options, cb) { From 5e1d7bc578a4ade4c1b8e6606896076f87ec0e30 Mon Sep 17 00:00:00 2001 From: kikuchan Date: Mon, 13 Sep 2021 15:07:18 +0900 Subject: [PATCH 2/2] fix(core): fix type check for Date prop --- src/core/util/props.js | 3 +++ src/shared/util.js | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/core/util/props.js b/src/core/util/props.js index 5a90f21b5f0..facea1fa5ba 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -8,6 +8,7 @@ import { toRawType, hyphenate, capitalize, + isDate, isPlainObject } from 'shared/util' @@ -166,6 +167,8 @@ function assertType (value: any, type: Function, vm: ?Component): { valid = isPlainObject(value) } else if (expectedType === 'Array') { valid = Array.isArray(value) + } else if (expectedType === 'Date') { + valid = isDate(value) } else { try { valid = value instanceof type diff --git a/src/shared/util.js b/src/shared/util.js index 9f240c77b14..06200ef2184 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -63,6 +63,10 @@ export function isRegExp (v: any): boolean { return _toString.call(v) === '[object RegExp]' } +export function isDate (v: any): boolean { + return v instanceof Date || _toString.call(v) === '[object Date]' +} + /** * Check if val is a valid array index. */ @@ -294,7 +298,7 @@ export function looseEqual (a: any, b: any): boolean { return a.length === b.length && a.every((e, i) => { return looseEqual(e, b[i]) }) - } else if (a instanceof Date && b instanceof Date) { + } else if (isDate(a) && isDate(b)) { return a.getTime() === b.getTime() } else if (!isArrayA && !isArrayB) { const keysA = Object.keys(a)