Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages-private/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,22 @@ describe('defineOptions', () => {
expose: ['expose'],
})
})

describe('defineEmits w/ type declaration (Issue #13935)', () => {
const emit = defineEmits<{
open: [payload: number]
close: [payload: string]
}>()

// Correct usages
emit('open', 123)
emit('close', 'abc')

// Incorrect usages (should fail type check)
// @ts-expect-error
emit('open', 'string')
// @ts-expect-error
emit('close', 123)
// @ts-expect-error
emit('unknown', 123)
})
12 changes: 4 additions & 8 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
type IfAny,
type LooseRequired,
type Prettify,
type UnionToIntersection,
extend,
isArray,
isFunction,
Expand Down Expand Up @@ -151,13 +150,10 @@ export function defineEmits() {

export type ComponentTypeEmits = ((...args: any[]) => any) | Record<string, any>

type RecordToUnion<T extends Record<string, any>> = T[keyof T]

type ShortEmits<T extends Record<string, any>> = UnionToIntersection<
RecordToUnion<{
[K in keyof T]: (evt: K, ...args: T[K]) => void
}>
>
type ShortEmits<T extends Record<string, any>> = <K extends keyof T>(
evt: K,
...args: T[K]
) => void

/**
* Vue `<script setup>` compiler macro for declaring a component's exposed
Expand Down