Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(defineSlots): Support for generic keyof slots #8374

Merged
merged 5 commits into from
Dec 8, 2023
Merged
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
96 changes: 96 additions & 0 deletions packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ describe('defineSlots', () => {
expectType<Slots>(slotsUntype)
})

describe('defineSlots generic', <T extends Record<string, any>>() => {
const props = defineProps<{
item: T
}>()

const slots = defineSlots<
{
[K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
} & {
label?: (props: { item: T }) => any
}
>()

for (const key of Object.keys(props.item) as (keyof T & string)[]) {
slots[`slot-${String(key)}`]?.({
item: props.item
})
}
slots.label?.({ item: props.item })

// @ts-expect-error calling wrong slot
slots.foo({})
})

describe('defineModel', () => {
// overload 1
const modelValueRequired = defineModel<boolean>({ required: true })
Expand Down Expand Up @@ -336,6 +360,78 @@ describe('useSlots', () => {
expectType<Slots>(slots)
})

describe('defineSlots generic', <T extends Record<string, any>>() => {
const props = defineProps<{
item: T
}>()

const slots = defineSlots<
{
[K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
} & {
label?: (props: { item: T }) => any
}
>()

// @ts-expect-error slots should be readonly
slots.label = () => {}

// @ts-expect-error non existing slot
slots['foo-asdas']?.({
item: props.item
})
for (const key in props.item) {
slots[`slot-${String(key)}`]?.({
item: props.item
})
slots[`slot-${String(key as keyof T)}`]?.({
item: props.item
})
}

for (const key of Object.keys(props.item) as (keyof T)[]) {
slots[`slot-${String(key)}`]?.({
item: props.item
})
}
slots.label?.({ item: props.item })

// @ts-expect-error calling wrong slot
slots.foo({})
})

describe('defineSlots generic strict', <T extends {
foo: 'foo'
bar: 'bar'
}>() => {
const props = defineProps<{
item: T
}>()

const slots = defineSlots<
{
[K in keyof T as `slot-${K & string}`]?: (props: { item: T }) => any
} & {
label?: (props: { item: T }) => any
}
>()

// slot-bar/foo should be automatically inferred
slots['slot-bar']?.({ item: props.item })
slots['slot-foo']?.({ item: props.item })

slots.label?.({ item: props.item })

// @ts-expect-error not part of the extends
slots['slot-RANDOM']?.({ item: props.item })

// @ts-expect-error slots should be readonly
slots.label = () => {}

// @ts-expect-error calling wrong slot
slots.foo({})
})

// #6420
describe('toRefs w/ type declaration', () => {
const props = defineProps<{
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type SlotsType<T extends Record<string, any> = Record<string, any>> = {
export type StrictUnwrapSlotsType<
S extends SlotsType,
T = NonNullable<S[typeof SlotSymbol]>
> = [keyof S] extends [never] ? Slots : Readonly<T>
> = [keyof S] extends [never] ? Slots : Readonly<T> & T

export type UnwrapSlotsType<
S extends SlotsType,
Expand Down
Loading