]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore(sfc-playground): ignore duplicate logs
authorEvan You <yyx990803@gmail.com>
Fri, 25 Jun 2021 23:31:47 +0000 (19:31 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 25 Jun 2021 23:31:47 +0000 (19:31 -0400)
packages/runtime-core/src/apiSetupHelpers.ts
packages/sfc-playground/src/output/Preview.vue
test-dts/setupHelpers.test-d.ts

index c1703f2233337055e00b624cc910c0abec187c91..ff770e3ffc4239a5b55861715f06ee0c33d088d9 100644 (file)
@@ -7,6 +7,12 @@ import { EmitFn, EmitsOptions } from './componentEmits'
 import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
 import { warn } from './warning'
 
+type InferDefaults<T> = {
+  [K in keyof T]?: NonNullable<T[K]> extends object
+    ? () => NonNullable<T[K]>
+    : NonNullable<T[K]>
+}
+
 /**
  * Compile-time-only helper used for declaring props inside `<script setup>`.
  * This is stripped away in the compiled code and should never be actually
@@ -25,7 +31,10 @@ export function defineProps<
   TypeProps = undefined,
   PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions,
   InferredProps = ExtractPropTypes<PP>
->(props?: PP): Readonly<TypeProps extends undefined ? InferredProps : TypeProps>
+>(
+  props?: PP,
+  defaults?: InferDefaults<TypeProps>
+): Readonly<TypeProps extends undefined ? InferredProps : TypeProps>
 // implementation
 export function defineProps() {
   if (__DEV__) {
index 4c5a41c8243f9a08cadc91240f0275beab2b9dea..726d354bfbf5f9a76308a7b78da33bc48fc77c7e 100644 (file)
@@ -123,6 +123,9 @@ function createSandbox() {
       runtimeError.value = 'Uncaught (in promise): ' + error.message
     },
     on_console: (log: any) => {
+      if (log.duplicate) {
+        return
+      }
       if (log.level === 'error') {
         if (log.args[0] instanceof Error) {
           runtimeError.value = log.args[0].message
@@ -156,7 +159,10 @@ function createSandbox() {
 }
 
 async function updatePreview() {
-  console.clear()
+  // @ts-ignore
+  if (import.meta.env.PROD) {
+    console.clear()
+  }
   runtimeError.value = null
   runtimeWarning.value = null
   try {
index fc4d801a510e8c305115019d050b52af7cce0d6e..66c8fe5a3c28de784f36e97dc93fe0b780f3470f 100644 (file)
@@ -19,6 +19,32 @@ describe('defineProps w/ type declaration', () => {
   props.bar
 })
 
+describe('defineProps w/ type declaration + defaults', () => {
+  defineProps<{
+    number?: number
+    arr?: string[]
+    arr2?: string[]
+    obj?: { x: number }
+    obj2?: { x: number }
+    obj3?: { x: number }
+  }>(
+    {},
+    {
+      number: 1,
+
+      arr: () => [''],
+      // @ts-expect-error not using factory
+      arr2: [''],
+
+      obj: () => ({ x: 123 }),
+      // @ts-expect-error not using factory
+      obj2: { x: 123 },
+      // @ts-expect-error factory return type does not match
+      obj3: () => ({ x: 'foo' })
+    }
+  )
+})
+
 describe('defineProps w/ runtime declaration', () => {
   // runtime declaration
   const props = defineProps({