Skip to content

Commit

Permalink
refactor: style
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 30, 2023
1 parent 3c6e028 commit 76b38c6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
15 changes: 6 additions & 9 deletions packages/dts-test/defineComponent.test-d.tsx
Expand Up @@ -1411,20 +1411,17 @@ export default {
describe('slots', () => {
defineComponent({
slots: Object as SlotsType<{
default: [foo: string, bar: number]
item: [number]
default: { foo: string; bar: number }
item: { data: number }
}>,
setup(props, { slots }) {
expectType<undefined | ((foo: string, bar: number) => any)>(slots.default)
expectType<undefined | ((scope: number) => any)>(slots.item)
expectType<undefined | ((scope: { foo: string; bar: number }) => any)>(
slots.default
)
expectType<undefined | ((scope: { data: number }) => any)>(slots.item)
}
})

defineComponent({
// @ts-expect-error `default` should be an array
slots: Object as SlotsType<{ default: string }>
})

defineComponent({
setup(props, { slots }) {
// unknown slots
Expand Down
6 changes: 4 additions & 2 deletions packages/dts-test/functionalComponent.test-d.tsx
Expand Up @@ -69,11 +69,13 @@ const Qux: FunctionalComponent<{}, ['foo', 'bar']> = (props, { emit }) => {

expectType<Component>(Qux)

const Quux: FunctionalComponent<{}, {}, { default: [foo: number] }> = (
const Quux: FunctionalComponent<{}, {}, { default: { foo: number } }> = (
props,
{ emit, slots }
) => {
expectType<{ default: undefined | ((foo: number) => VNode[]) }>(slots)
expectType<{ default: undefined | ((scope: { foo: number }) => VNode[]) }>(
slots
)
}
expectType<Component>(Quux)
;<Quux />
7 changes: 2 additions & 5 deletions packages/dts-test/setupHelpers.test-d.ts
Expand Up @@ -164,15 +164,12 @@ describe('defineEmits w/ runtime declaration', () => {

describe('defineSlots', () => {
const slots = defineSlots<{
default: [foo: string, bar: number]
default: { foo: string; bar: number }
}>()
expectType<(foo: string, bar: number) => VNode[]>(slots.default)
expectType<(scope: { foo: string; bar: number }) => VNode[]>(slots.default)

const slotsUntype = defineSlots()
expectType<Slots>(slotsUntype)

// @ts-expect-error `default` should be an array
defineSlots<{ default: string }>()
})

describe('useAttrs', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiSetupHelpers.ts
Expand Up @@ -174,10 +174,10 @@ export function defineOptions<
}

export function defineSlots<
T extends Record<string, any[]> = Record<string, any[]>
T extends Record<string, any> = Record<string, any>
>(): // @ts-expect-error
Readonly<{
[K in keyof T]: Slot<T[K]>
[K in keyof T]: Slot<[T[K]]>
}> {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/component.ts
Expand Up @@ -125,7 +125,7 @@ export interface ComponentInternalOptions {
export interface FunctionalComponent<
P = {},
E extends EmitsOptions = {},
S extends Record<string, any[]> = Record<string, any[]>
S extends Record<string, any> = Record<string, any>
> extends ComponentInternalOptions {
// use of any here is intentional so it can be a valid JSX Element constructor
(props: P, ctx: Omit<SetupContext<E, SlotsType<S>>, 'expose'>): any
Expand Down
13 changes: 7 additions & 6 deletions packages/runtime-core/src/componentSlots.ts
Expand Up @@ -23,7 +23,9 @@ import { isHmrUpdating } from './hmr'
import { DeprecationTypes, isCompatEnabled } from './compat/compatConfig'
import { toRaw } from '@vue/reactivity'

export type Slot<T extends any[] = any[]> = (...args: T) => VNode[]
export type Slot<T extends any[] = any[]> = (
...args: T extends [any] ? any[] : T
) => VNode[]

export type InternalSlots = {
[name: string]: Slot | undefined
Expand All @@ -32,17 +34,16 @@ export type InternalSlots = {
export type Slots = Readonly<InternalSlots>

declare const SlotSymbol: unique symbol
export type SlotsType<T extends Record<string, any[]> = Record<string, any[]>> =
{
[SlotSymbol]?: T
}
export type SlotsType<T extends Record<string, any> = Record<string, any>> = {
[SlotSymbol]?: T
}

export type TypedSlots<S extends SlotsType> = [keyof S] extends [never]
? Slots
: Readonly<
Prettify<{
[K in keyof NonNullable<S[typeof SlotSymbol]>]:
| Slot<NonNullable<S[typeof SlotSymbol]>[K]>
| Slot<[NonNullable<S[typeof SlotSymbol]>[K]]>
| undefined
}>
>
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/h.ts
Expand Up @@ -123,7 +123,7 @@ export function h(
export function h<
P,
E extends EmitsOptions = {},
S extends Record<string, any[]> = {}
S extends Record<string, any> = {}
>(
type: FunctionalComponent<P, E, S>,
props?: (RawProps & P) | ({} extends P ? null : never),
Expand Down

0 comments on commit 76b38c6

Please sign in to comment.