Skip to content

Commit

Permalink
chore(sfc-playground): ignore duplicate logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 25, 2021
1 parent 1675b6d commit 3ffc7be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__) {
Expand Down
8 changes: 7 additions & 1 deletion packages/sfc-playground/src/output/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions test-dts/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 3ffc7be

Please sign in to comment.