})
describe('defineSlots', () => {
- // short syntax
- const slots = defineSlots<{
- default: { foo: string; bar: number }
- optional?: string
- }>()
- expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default)
- expectType<undefined | ((scope: string) => VNode[])>(slots.optional)
-
// literal fn syntax (allow for specifying return type)
const fnSlots = defineSlots<{
default(props: { foo: string; bar: number }): any
PropOptions
} from './componentProps'
import { warn } from './warning'
-import { SlotsType, TypedSlots } from './componentSlots'
+import { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
import { Ref, ref } from '@vue/reactivity'
import { watch } from './apiWatch'
export function defineSlots<
S extends Record<string, any> = Record<string, any>
->(): TypedSlots<SlotsType<S>> {
+>(): StrictUnwrapSlotsType<SlotsType<S>> {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
}
InternalSlots,
Slots,
SlotsType,
- TypedSlots
+ UnwrapSlotsType
} from './componentSlots'
import { warn } from './warning'
import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
> = E extends any
? {
attrs: Data
- slots: TypedSlots<S>
+ slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: (exposed?: Record<string, any>) => void
}
ComponentInjectOptions
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
-import { SlotsType, TypedSlots } from './componentSlots'
+import { SlotsType, UnwrapSlotsType } from './componentSlots'
import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext'
import { warn } from './warning'
>
$attrs: Data
$refs: Data
- $slots: TypedSlots<S>
+ $slots: UnwrapSlotsType<S>
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
$emit: EmitFn<E>
[SlotSymbol]?: T
}
-export type TypedSlots<
+export type StrictUnwrapSlotsType<
+ S extends SlotsType,
+ T = NonNullable<S[typeof SlotSymbol]>
+> = [keyof S] extends [never] ? Slots : Readonly<T>
+
+export type UnwrapSlotsType<
S extends SlotsType,
T = NonNullable<S[typeof SlotSymbol]>
> = [keyof S] extends [never]