Skip to content
Open
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
12 changes: 12 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,18 @@ describe('componentOptions setup should be `SetupContext`', () => {
)
})

describe('infer emits from SetupContext', () => {
// options
const Foo = defineComponent({
setup(_, _ctx: SetupContext<['foo']>) {},
})
;<Foo onFoo={() => {}}></Foo>

// functional
const Bar = defineComponent((_, _ctx: SetupContext<['foo']>) => () => {})
;<Bar onFoo={() => {}}></Bar>
})

describe('extract instance type', () => {
const Base = defineComponent({
props: {
Expand Down
22 changes: 8 additions & 14 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,14 @@ export type { ComponentOptions }

export type LifecycleHook<TFn = Function> = (TFn & SchedulerJob)[] | null

// use `E extends any` to force evaluating type to fix #2362
export type SetupContext<
E = EmitsOptions,
S extends SlotsType = {},
> = E extends any
? {
attrs: Attrs
slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: <Exposed extends Record<string, any> = Record<string, any>>(
exposed?: Exposed,
) => void
}
: never
export type SetupContext<E = EmitsOptions, S extends SlotsType = {}> = {
attrs: Attrs
slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: <Exposed extends Record<string, any> = Record<string, any>>(
exposed?: Exposed,
) => void
}
Comment thread
zhiyuanzmj marked this conversation as resolved.

/**
* @internal
Expand Down
Loading