Skip to content

Commit 232f402

Browse files
authored
fix(types): support named tuple emits (#12676)
close #12673
1 parent 671997a commit 232f402

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎packages-private/dts-test/defineComponent.test-d.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,29 @@ describe('function syntax w/ emits', () => {
13971397
},
13981398
},
13991399
)
1400+
1401+
const NamedTupleEmit = defineComponent<
1402+
{},
1403+
{
1404+
update: [value: string] // named tuple syntax
1405+
}
1406+
>((_props, ctx) => {
1407+
ctx.emit('update', '123')
1408+
// @ts-expect-error
1409+
ctx.emit('update', 123)
1410+
// @ts-expect-error
1411+
ctx.emit('non-exist')
1412+
return () => {}
1413+
})
1414+
expectType<JSX.Element>(
1415+
<NamedTupleEmit
1416+
onUpdate={value => {
1417+
expectType<string>(value.toUpperCase())
1418+
// @ts-expect-error string payload should not expose number methods
1419+
value.toFixed()
1420+
}}
1421+
/>,
1422+
)
14001423
})
14011424

14021425
describe('function syntax w/ runtime props', () => {

‎packages/runtime-core/src/componentEmits.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import type { ComponentPublicInstance } from './componentPublicInstance'
3535

3636
export type ObjectEmitsOptions = Record<
3737
string,
38-
((...args: any[]) => any) | null
38+
((...args: any[]) => any) | null | any[]
3939
>
4040

4141
export type EmitsOptions = ObjectEmitsOptions | string[]
@@ -52,7 +52,9 @@ export type EmitsToProps<T extends EmitsOptions | ComponentTypeEmits> =
5252
? P
5353
: T[K] extends null
5454
? any[]
55-
: never
55+
: T[K] extends any[]
56+
? T[K]
57+
: never
5658
) => any
5759
}
5860
: {}

0 commit comments

Comments
 (0)