Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
71 changes: 71 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,77 @@ describe('componentOptions setup should be `SetupContext`', () => {
)
})

describe('infer slots from `SetupContext`', () => {
// options
const Foo = defineComponent({
setup(
_props,
_ctx: SetupContext<
EmitsOptions,
{
default: (props: { foo: number }) => any
}
>,
) {},
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: SetupContext<
EmitsOptions,
{
default: (props: { foo: number }) => any
}
>,
) {},
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: {
slots: {
default: (props: { foo: T }) => any
}
},
) =>
() => [],
)
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: SetupContext<
EmitsOptions,
{
default: (props: { foo: T }) => any
}
>,
) =>
() => [],
)
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
14 changes: 7 additions & 7 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type {
ComponentPublicInstanceConstructor,
CreateComponentPublicInstanceWithMixins,
} from './componentPublicInstance'
import type { SlotsType } from './componentSlots'
import type { Slots, SlotsType } from './componentSlots'
import type { Directive } from './directives'
import type { ComponentTypeEmits } from './apiSetupHelpers'

Expand Down 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 | Slots = {},
>(
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 | Slots = {},
>(
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 = {},
TypeSlots extends SlotsType | Slots = {},
LocalComponents extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Expand Down Expand Up @@ -249,7 +249,7 @@ export function defineComponent<
{}, // Defaults
InjectOptions,
InjectKeys,
Slots,
TypeSlots,
LocalComponents,
Directives,
Exposed,
Expand All @@ -269,7 +269,7 @@ export function defineComponent<
{},
false,
InjectOptions,
Slots,
TypeSlots,
LocalComponents,
Directives,
string
Expand All @@ -288,7 +288,7 @@ export function defineComponent<
PublicProps,
ToResolvedProps<InferredProps, ResolvedEmits>,
ExtractDefaultPropTypes<RuntimePropsOptions>,
Slots,
TypeSlots,
LocalComponents,
Directives,
Exposed,
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 | Slots = {},
> = 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
6 changes: 3 additions & 3 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
shouldCacheAccess,
} from './componentOptions'
import type { EmitFn, EmitsOptions } from './componentEmits'
import type { SlotsType, UnwrapSlotsType } from './componentSlots'
import type { Slots, SlotsType, UnwrapSlotsType } from './componentSlots'
import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext'
import { warn } from './warning'
Expand Down Expand Up @@ -224,7 +224,7 @@ export type CreateComponentPublicInstanceWithMixins<
Defaults = {},
MakeDefaultsOptional extends boolean = false,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
S extends SlotsType | Slots = {},
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