fix(types): remove short syntax support in defineSlots()

ref: https://github.com/vuejs/language-tools/issues/2758
This commit is contained in:
Evan You 2023-05-08 11:53:49 +08:00
parent 862edfd91a
commit 1279b17300
5 changed files with 12 additions and 15 deletions

View File

@ -186,14 +186,6 @@ describe('defineEmits w/ runtime declaration', () => {
}) })
describe('defineSlots', () => { 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) // literal fn syntax (allow for specifying return type)
const fnSlots = defineSlots<{ const fnSlots = defineSlots<{
default(props: { foo: string; bar: number }): any default(props: { foo: string; bar: number }): any

View File

@ -28,7 +28,7 @@ import {
PropOptions PropOptions
} from './componentProps' } from './componentProps'
import { warn } from './warning' import { warn } from './warning'
import { SlotsType, TypedSlots } from './componentSlots' import { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
import { Ref, ref } from '@vue/reactivity' import { Ref, ref } from '@vue/reactivity'
import { watch } from './apiWatch' import { watch } from './apiWatch'
@ -205,7 +205,7 @@ export function defineOptions<
export function defineSlots< export function defineSlots<
S extends Record<string, any> = Record<string, any> S extends Record<string, any> = Record<string, any>
>(): TypedSlots<SlotsType<S>> { >(): StrictUnwrapSlotsType<SlotsType<S>> {
if (__DEV__) { if (__DEV__) {
warnRuntimeUsage(`defineSlots`) warnRuntimeUsage(`defineSlots`)
} }

View File

@ -32,7 +32,7 @@ import {
InternalSlots, InternalSlots,
Slots, Slots,
SlotsType, SlotsType,
TypedSlots UnwrapSlotsType
} from './componentSlots' } from './componentSlots'
import { warn } from './warning' import { warn } from './warning'
import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling' import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
@ -188,7 +188,7 @@ export type SetupContext<
> = E extends any > = E extends any
? { ? {
attrs: Data attrs: Data
slots: TypedSlots<S> slots: UnwrapSlotsType<S>
emit: EmitFn<E> emit: EmitFn<E>
expose: (exposed?: Record<string, any>) => void expose: (exposed?: Record<string, any>) => void
} }

View File

@ -40,7 +40,7 @@ import {
ComponentInjectOptions ComponentInjectOptions
} from './componentOptions' } from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits' import { EmitsOptions, EmitFn } from './componentEmits'
import { SlotsType, TypedSlots } from './componentSlots' import { SlotsType, UnwrapSlotsType } from './componentSlots'
import { markAttrsAccessed } from './componentRenderUtils' import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext' import { currentRenderingInstance } from './componentRenderContext'
import { warn } from './warning' import { warn } from './warning'
@ -213,7 +213,7 @@ export type ComponentPublicInstance<
> >
$attrs: Data $attrs: Data
$refs: Data $refs: Data
$slots: TypedSlots<S> $slots: UnwrapSlotsType<S>
$root: ComponentPublicInstance | null $root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null $parent: ComponentPublicInstance | null
$emit: EmitFn<E> $emit: EmitFn<E>

View File

@ -41,7 +41,12 @@ export type SlotsType<T extends Record<string, any> = Record<string, any>> = {
[SlotSymbol]?: T [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, S extends SlotsType,
T = NonNullable<S[typeof SlotSymbol]> T = NonNullable<S[typeof SlotSymbol]>
> = [keyof S] extends [never] > = [keyof S] extends [never]