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: convert emits to props, fix #13104 #13105

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions types/test/v3/define-component-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,16 @@ describe('emits', () => {
}
}
})

// should have `onXXX` props for emits
defineComponent({
emits: {
foo: (n: number) => n > 0
},
setup(props) {
expectType<((n: number) => boolean) | undefined>(props.onFoo)
}
})
})

// describe('componentOptions setup should be `SetupContext`', () => {
Expand Down
18 changes: 13 additions & 5 deletions types/v3-component-options.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vue } from './vue'
import { VNode } from './vnode'
import { ComponentOptions as Vue2ComponentOptions } from './options'
import { EmitsOptions, SetupContext } from './v3-setup-context'
import { EmitsOptions, EmitsToProps, SetupContext } from './v3-setup-context'
import { Data, LooseRequired, UnionToIntersection } from './common'
import {
ComponentPropsOptions,
Expand Down Expand Up @@ -137,6 +137,13 @@ export type ExtractComputedReturns<T extends any> = {
: never
}

export type ResolveProps<PropsOrPropOptions, E extends EmitsOptions> = Readonly<
PropsOrPropOptions extends ComponentPropsOptions
? ExtractPropTypes<PropsOrPropOptions>
: PropsOrPropOptions
> &
({} extends E ? {} : EmitsToProps<E>)

export type ComponentOptionsWithProps<
PropsOptions = ComponentPropsOptions,
RawBindings = Data,
Expand All @@ -147,7 +154,7 @@ export type ComponentOptionsWithProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {},
EmitsNames extends string = string,
Props = ExtractPropTypes<PropsOptions>,
Props = ResolveProps<PropsOptions, Emits>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Props,
Expand Down Expand Up @@ -185,7 +192,7 @@ export type ComponentOptionsWithArrayProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {},
EmitsNames extends string = string,
Props = Readonly<{ [key in PropNames]?: any }>
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<Emits>
> = ComponentOptionsBase<
Props,
RawBindings,
Expand Down Expand Up @@ -221,9 +228,10 @@ export type ComponentOptionsWithoutProps<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {},
EmitsNames extends string = string
EmitsNames extends string = string,
PropsWithEmits = Props & EmitsToProps<Emits>
> = ComponentOptionsBase<
Props,
PropsWithEmits,
RawBindings,
D,
C,
Expand Down
5 changes: 3 additions & 2 deletions types/v3-define-component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
ComponentOptionsWithArrayProps,
ComponentOptionsWithProps,
ComponentOptionsMixin,
ComponentOptionsBase
ComponentOptionsBase,
ResolveProps
} from './v3-component-options'
import {
ComponentPublicInstanceConstructor,
Expand Down Expand Up @@ -161,7 +162,7 @@ export function defineComponent<
Extends,
Emits,
EmitsNames,
Props
ResolveProps<PropsOptions, Emits>
>
: { functional?: never } & ComponentOptionsWithProps<
PropsOptions,
Expand Down
19 changes: 19 additions & 0 deletions types/v3-setup-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ export type ObjectEmitsOptions = Record<

export type EmitsOptions = ObjectEmitsOptions | string[]

export type EmitsToProps<T extends EmitsOptions> = T extends string[]
? {
[K in string & `on${Capitalize<T[number]>}`]?: (...args: any[]) => any
}
: T extends ObjectEmitsOptions
? {
[K in string &
`on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
? T[Uncapitalize<C>] extends null
? (...args: any[]) => any
: (
...args: T[Uncapitalize<C>] extends (...args: infer P) => any
? P
: never
) => any
: never
}
: {}

export type EmitFn<
Options = ObjectEmitsOptions,
Event extends keyof Options = keyof Options,
Expand Down