fix(types): fix tsx emit-mapped handler return type (#4290)

fix #4288
This commit is contained in:
webfansplz 2021-08-11 22:52:36 +08:00 committed by GitHub
parent 380608bd44
commit 1ce34e25d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -44,7 +44,11 @@ export type EmitsToProps<T extends EmitsOptions> = T extends string[]
`on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
? T[Uncapitalize<C>] extends null
? (...args: any[]) => any
: T[Uncapitalize<C>]
: (
...args: T[Uncapitalize<C>] extends (...args: infer P) => any
? P
: never
) => any
: never
}
: {}

View File

@ -966,6 +966,33 @@ describe('emits', () => {
}
})
// with tsx
const Component = defineComponent({
emits: {
click: (n: number) => typeof n === 'number'
},
setup(props, { emit }) {
expectType<((n: number) => any) | undefined>(props.onClick)
emit('click', 1)
// @ts-expect-error
expectError(emit('click'))
// @ts-expect-error
expectError(emit('click', 'foo'))
}
})
defineComponent({
render() {
return (
<Component
onClick={(n: number) => {
return n + 1
}}
/>
)
}
})
// without emits
defineComponent({
setup(props, { emit }) {