Skip to content

Commit

Permalink
fix(types): remove short syntax support in defineSlots()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 8, 2023
1 parent 862edfd commit 1279b17
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
8 changes: 0 additions & 8 deletions packages/dts-test/setupHelpers.test-d.ts
Expand Up @@ -186,14 +186,6 @@ describe('defineEmits w/ runtime declaration', () => {
})

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
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiSetupHelpers.ts
Expand Up @@ -28,7 +28,7 @@ import {
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'

Expand Down Expand Up @@ -205,7 +205,7 @@ export function defineOptions<

export function defineSlots<
S extends Record<string, any> = Record<string, any>
>(): TypedSlots<SlotsType<S>> {
>(): StrictUnwrapSlotsType<SlotsType<S>> {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/component.ts
Expand Up @@ -32,7 +32,7 @@ import {
InternalSlots,
Slots,
SlotsType,
TypedSlots
UnwrapSlotsType
} from './componentSlots'
import { warn } from './warning'
import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
Expand Down Expand Up @@ -188,7 +188,7 @@ export type SetupContext<
> = E extends any
? {
attrs: Data
slots: TypedSlots<S>
slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: (exposed?: Record<string, any>) => void
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Expand Up @@ -40,7 +40,7 @@ import {
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'
Expand Down Expand Up @@ -213,7 +213,7 @@ export type ComponentPublicInstance<
>
$attrs: Data
$refs: Data
$slots: TypedSlots<S>
$slots: UnwrapSlotsType<S>
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
$emit: EmitFn<E>
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-core/src/componentSlots.ts
Expand Up @@ -41,7 +41,12 @@ export type SlotsType<T extends Record<string, any> = Record<string, any>> = {
[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]
Expand Down

0 comments on commit 1279b17

Please sign in to comment.