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(type): convert emits to props #13133

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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(type): convert emits to props
  • Loading branch information
zcf0508 committed Dec 15, 2023
commit b44003eb049853611936dea2bc3168d745c93d6f
13 changes: 13 additions & 0 deletions types/test/v3/define-component-test.tsx
Original file line number Diff line number Diff line change
@@ -966,6 +966,19 @@ describe('emits', () => {
}
}
})

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

// describe('componentOptions setup should be `SetupContext`', () => {
4 changes: 2 additions & 2 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,
@@ -52,7 +52,7 @@ export type SetupFunction<
Emits extends EmitsOptions = {}
> = (
this: void,
props: Readonly<Props>,
props: Readonly<Props & EmitsToProps<Emits>>,
ctx: SetupContext<Emits>
) => RawBindings | (() => VNode | null) | void

4 changes: 2 additions & 2 deletions types/v3-component-public-instance.d.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import {
ComponentOptionsMixin,
ComponentOptionsBase
} from './v3-component-options'
import { EmitFn, EmitsOptions } from './v3-setup-context'
import { EmitFn, EmitsOptions, EmitsToProps } from './v3-setup-context'

/**
* Custom properties added to component instances in any way and can be accessed through `this`
@@ -150,7 +150,7 @@ export type ComponentPublicInstance<
>
> = Vue3Instance<
D,
P,
P & EmitsToProps<E>,
PublicProps,
E,
Defaults,
19 changes: 19 additions & 0 deletions types/v3-setup-context.d.ts
Original file line number Diff line number Diff line change
@@ -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,