Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): correct type inference for PascalCase emits #11579

Merged
merged 5 commits into from
Aug 15, 2024
Merged
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
15 changes: 15 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,15 @@ describe('emits', () => {
emits: {
click: (n: number) => typeof n === 'number',
input: (b: string) => b.length > 1,
Focus: (f: boolean) => !!f,
},
setup(props, { emit }) {
expectType<((n: number) => boolean) | undefined>(props.onClick)
expectType<((b: string) => boolean) | undefined>(props.onInput)
expectType<((f: boolean) => boolean) | undefined>(props.onFocus)
emit('click', 1)
emit('input', 'foo')
emit('Focus', true)
// @ts-expect-error
emit('nope')
// @ts-expect-error
Expand All @@ -922,6 +925,10 @@ describe('emits', () => {
emit('input')
// @ts-expect-error
emit('input', 1)
// @ts-expect-error
emit('focus')
// @ts-expect-error
emit('focus', true)
},
created() {
this.$emit('click', 1)
Expand All @@ -936,6 +943,10 @@ describe('emits', () => {
this.$emit('input')
// @ts-expect-error
this.$emit('input', 1)
// @ts-expect-error
this.$emit('focus')
// @ts-expect-error
this.$emit('focus', true)
},
mounted() {
// #3599
Expand All @@ -954,6 +965,10 @@ describe('emits', () => {
this.$emit('input')
// @ts-expect-error
this.$emit('input', 1)
// @ts-expect-error
this.$emit('focus')
// @ts-expect-error
this.$emit('focus', true)
})
},
})
Expand Down
16 changes: 7 additions & 9 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ export type EmitsToProps<T extends EmitsOptions | ComponentTypeEmits> =
}
: T extends ObjectEmitsOptions
? {
[K in `on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
? (
...args: T[Uncapitalize<C>] extends (...args: infer P) => any
? P
: T[Uncapitalize<C>] extends null
? any[]
: never
) => any
: never
[K in string & keyof T as `on${Capitalize<K>}`]?: (
...args: T[K] extends (...args: infer P) => any
? P
: T[K] extends null
? any[]
: never
) => any
}
: {}

Expand Down