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
69 changes: 68 additions & 1 deletion packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type Component,
type ComponentOptions,
type ComponentPublicInstance,
type EmitsOptions,
type PropType,
type SetupContext,
type Slots,
Expand Down Expand Up @@ -1175,6 +1176,73 @@ describe('componentOptions setup should be `SetupContext`', () => {
)
})

describe('infer slots from `SetupContext`', () => {
// options
const Foo = defineComponent({
setup(
_props,
_ctx: SetupContext<EmitsOptions, { default: { foo: number } }>,
) {},
render() {
this.$slots.default({ foo: 1 })
},
})
const foo = {} as InstanceType<typeof Foo>
expectType<IsAny<typeof foo.$slots.default>>(false)
foo.$slots.default({ foo: 1 })

Comment thread
zhiyuanzmj marked this conversation as resolved.
const Bar = defineComponent({
setup(
_props,
_ctx: {
slots: {
default: (props: { foo: number }) => VNode
}
},
) {},
render() {
this.$slots.default({ foo: 1 })
},
})
const bar = {} as InstanceType<typeof Bar>
expectType<IsAny<typeof bar.$slots.default>>(false)
bar.$slots.default({ foo: 1 })

// functional
const Baz = defineComponent(
<T,>(
props: { foo: T },
ctx: SetupContext<
EmitsOptions,
SlotsType<{
default: (props: { foo: T }) => VNode
}>
>,
) =>
() =>
ctx.slots.default(props),
)
const baz = new Baz({ foo: 1 })
expectType<IsAny<typeof baz.$slots.default>>(false)
baz.$slots.default({ foo: 1 })

const Qux = defineComponent(
<T,>(
props: { foo: T },
ctx: {
slots: {
default: (props: { foo: T }) => VNode
}
},
) =>
() =>
ctx.slots.default(props),
)
const qux = new Qux({ foo: 1 })
expectType<IsAny<typeof qux.$slots.default>>(false)
qux.$slots.default({ foo: 1 })
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.

describe('extract instance type', () => {
const Base = defineComponent({
props: {
Expand Down Expand Up @@ -1750,7 +1818,6 @@ import type {
ComponentOptionsMixin,
DefineComponent,
Directive,
EmitsOptions,
ExtractPropTypes,
KeepAliveProps,
TransitionProps,
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function defineComponent<
Props extends Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
S extends SlotsType | Record<string, any> = {},
>(
setup: (
props: Props,
Expand All @@ -166,7 +166,7 @@ export function defineComponent<
Props extends Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
S extends SlotsType | Record<string, any> = {},
>(
setup: (
props: Props,
Expand Down Expand Up @@ -199,7 +199,7 @@ export function defineComponent<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
InjectOptions extends ComponentInjectOptions = {},
InjectKeys extends string = string,
Slots extends SlotsType = {},
Slots extends SlotsType | Record<string, any> = {},
LocalComponents extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ 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 = {},
S extends SlotsType | Record<string, any> = {},
> = E extends any
? {
attrs: Attrs
slots: UnwrapSlotsType<S>
slots: S extends SlotsType ? UnwrapSlotsType<S> : Readonly<S>
Comment thread
zhiyuanzmj marked this conversation as resolved.
emit: EmitFn<E>
expose: <Exposed extends Record<string, any> = Record<string, any>>(
exposed?: Exposed,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export type CreateComponentPublicInstanceWithMixins<
Defaults = {},
MakeDefaultsOptional extends boolean = false,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
S extends SlotsType | Record<string, any> = {},
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Expand Down Expand Up @@ -272,7 +272,7 @@ export type CreateComponentPublicInstanceWithMixins<
Provide
>,
I,
S,
S extends SlotsType ? S : SlotsType<S>,
Exposed,
Comment thread
zhiyuanzmj marked this conversation as resolved.
TypeRefs,
TypeEl
Expand Down
Loading