Skip to content

Commit 1279b17

Browse files
committed
fix(types): remove short syntax support in defineSlots()
ref: vuejs/language-tools#2758
1 parent 862edfd commit 1279b17

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

packages/dts-test/setupHelpers.test-d.ts

-8
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,6 @@ describe('defineEmits w/ runtime declaration', () => {
186186
})
187187

188188
describe('defineSlots', () => {
189-
// short syntax
190-
const slots = defineSlots<{
191-
default: { foo: string; bar: number }
192-
optional?: string
193-
}>()
194-
expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default)
195-
expectType<undefined | ((scope: string) => VNode[])>(slots.optional)
196-
197189
// literal fn syntax (allow for specifying return type)
198190
const fnSlots = defineSlots<{
199191
default(props: { foo: string; bar: number }): any

packages/runtime-core/src/apiSetupHelpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
PropOptions
2929
} from './componentProps'
3030
import { warn } from './warning'
31-
import { SlotsType, TypedSlots } from './componentSlots'
31+
import { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
3232
import { Ref, ref } from '@vue/reactivity'
3333
import { watch } from './apiWatch'
3434

@@ -205,7 +205,7 @@ export function defineOptions<
205205

206206
export function defineSlots<
207207
S extends Record<string, any> = Record<string, any>
208-
>(): TypedSlots<SlotsType<S>> {
208+
>(): StrictUnwrapSlotsType<SlotsType<S>> {
209209
if (__DEV__) {
210210
warnRuntimeUsage(`defineSlots`)
211211
}

packages/runtime-core/src/component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
InternalSlots,
3333
Slots,
3434
SlotsType,
35-
TypedSlots
35+
UnwrapSlotsType
3636
} from './componentSlots'
3737
import { warn } from './warning'
3838
import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
@@ -188,7 +188,7 @@ export type SetupContext<
188188
> = E extends any
189189
? {
190190
attrs: Data
191-
slots: TypedSlots<S>
191+
slots: UnwrapSlotsType<S>
192192
emit: EmitFn<E>
193193
expose: (exposed?: Record<string, any>) => void
194194
}

packages/runtime-core/src/componentPublicInstance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
ComponentInjectOptions
4141
} from './componentOptions'
4242
import { EmitsOptions, EmitFn } from './componentEmits'
43-
import { SlotsType, TypedSlots } from './componentSlots'
43+
import { SlotsType, UnwrapSlotsType } from './componentSlots'
4444
import { markAttrsAccessed } from './componentRenderUtils'
4545
import { currentRenderingInstance } from './componentRenderContext'
4646
import { warn } from './warning'
@@ -213,7 +213,7 @@ export type ComponentPublicInstance<
213213
>
214214
$attrs: Data
215215
$refs: Data
216-
$slots: TypedSlots<S>
216+
$slots: UnwrapSlotsType<S>
217217
$root: ComponentPublicInstance | null
218218
$parent: ComponentPublicInstance | null
219219
$emit: EmitFn<E>

packages/runtime-core/src/componentSlots.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ export type SlotsType<T extends Record<string, any> = Record<string, any>> = {
4141
[SlotSymbol]?: T
4242
}
4343

44-
export type TypedSlots<
44+
export type StrictUnwrapSlotsType<
45+
S extends SlotsType,
46+
T = NonNullable<S[typeof SlotSymbol]>
47+
> = [keyof S] extends [never] ? Slots : Readonly<T>
48+
49+
export type UnwrapSlotsType<
4550
S extends SlotsType,
4651
T = NonNullable<S[typeof SlotSymbol]>
4752
> = [keyof S] extends [never]

0 commit comments

Comments
 (0)