Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: convert emits to props
  • Loading branch information
so1ve committed Oct 23, 2023
commit 4b9adccfad9a614c7e4099ce9351ce9fecd756a9
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