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
33 changes: 33 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type ComponentOptions,
type ComponentPublicInstance,
type PropType,
type Ref,
type SetupContext,
type Slots,
type SlotsType,
Expand All @@ -12,6 +13,7 @@ import {
h,
reactive,
ref,
shallowRef,
withKeys,
withModifiers,
} from 'vue'
Expand Down Expand Up @@ -1175,6 +1177,37 @@ describe('componentOptions setup should be `SetupContext`', () => {
)
})

describe('infer expose from `SetupContext`', () => {
// functional
const Baz = defineComponent(
<T,>(
props: { foo: T },
ctx: SetupContext<EmitsOptions, {}, { bar: Ref<T> }>,
) => {
ctx.expose({ bar: shallowRef(props.foo) })
return () => <></>
},
)
const baz = new Baz({ foo: 1 })
expectType<IsAny<typeof baz.bar>>(false)
expectType<number>(baz.bar)

const Qux = defineComponent(
<T,>(
props: { foo: T },
ctx: {
expose: (exposed: { bar: Ref<T> }) => void
},
) => {
ctx.expose({ bar: shallowRef(props.foo) })
return () => <></>
},
)
const qux = new Qux({ foo: 1 })
expectType<IsAny<typeof qux.bar>>(false)
expectType<number>(qux.bar)
})

describe('extract instance type', () => {
const Base = defineComponent({
props: {
Expand Down
13 changes: 8 additions & 5 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ export type DefineSetupFnComponent<
P extends Record<string, any>,
E extends EmitsOptions = {},
S extends SlotsType = SlotsType,
B extends Record<string, any> = {},
Props = P & EmitsToProps<E>,
PP = PublicProps,
> = new (
props: Props & PP,
) => CreateComponentPublicInstanceWithMixins<
Props,
{},
B,
{},
{},
{},
Expand Down Expand Up @@ -151,33 +152,35 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
B extends Record<string, any> = {},
>(
setup: (
props: Props,
ctx: SetupContext<E, S>,
ctx: SetupContext<E, S, B>,
) => RenderFunction | Promise<RenderFunction>,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
props?: (keyof NoInfer<Props>)[]
emits?: E | EE[]
slots?: S
},
): DefineSetupFnComponent<Props, E, S>
): DefineSetupFnComponent<Props, E, S, B>
export function defineComponent<
Props extends Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
B extends Record<string, any> = {},
>(
setup: (
props: Props,
ctx: SetupContext<E, S>,
ctx: SetupContext<E, S, B>,
) => RenderFunction | Promise<RenderFunction>,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
props?: ComponentObjectPropsOptions<Props>
emits?: E | EE[]
slots?: S
},
): DefineSetupFnComponent<Props, E, S>
): DefineSetupFnComponent<Props, E, S, B>

// overload 2: defineComponent with options object, infer props from options
export function defineComponent<
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,13 @@ export type LifecycleHook<TFn = Function> = (TFn & SchedulerJob)[] | null
export type SetupContext<
E = EmitsOptions,
S extends SlotsType = {},
Exposed extends Record<string, any> = Record<string, any>,
> = E extends any
? {
attrs: Attrs
slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: <Exposed extends Record<string, any> = Record<string, any>>(
exposed?: Exposed,
) => void
expose: (exposed?: Exposed) => void
}
: never

Expand Down
Loading