From 9c0e44b67ec9eddf2d754fe2e2dcfcfb26ec7b3f Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 21 Jun 2024 14:37:14 -0400 Subject: [PATCH] Simplify props types wip wip --- .../src/components/transition/transition.tsx | 6 ++---- .../src/utils/render.test.tsx | 6 ++++-- packages/@headlessui-react/src/utils/render.ts | 18 +++++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/@headlessui-react/src/components/transition/transition.tsx b/packages/@headlessui-react/src/components/transition/transition.tsx index d2d4189ab4..a911b4919f 100644 --- a/packages/@headlessui-react/src/components/transition/transition.tsx +++ b/packages/@headlessui-react/src/components/transition/transition.tsx @@ -318,7 +318,6 @@ function TransitionChildFn(null) @@ -444,6 +443,8 @@ function TransitionChildFn, ref: Ref ) { - // @ts-expect-error let { show, appear = false, unmount = true, ...theirProps } = props as typeof props let internalTransitionRef = useRef(null) let requiresRef = shouldForwardRef(props) @@ -610,10 +610,8 @@ function ChildFn return ( <> {!hasTransitionContext && hasOpenClosedContext ? ( - // @ts-expect-error This is an object ) : ( - // @ts-expect-error This is an object )} diff --git a/packages/@headlessui-react/src/utils/render.test.tsx b/packages/@headlessui-react/src/utils/render.test.tsx index f6844c5751..ac28cad01e 100644 --- a/packages/@headlessui-react/src/utils/render.test.tsx +++ b/packages/@headlessui-react/src/utils/render.test.tsx @@ -424,9 +424,11 @@ describe('Features.Static | Features.RenderStrategy', () => { } // TODO: Can we "legit" test this? 🤔 - it('should result in a typescript error', () => { + it.skip('should result in a typescript error', () => { testRender( - // @ts-expect-error static & unmount together are incompatible + // Properly setting up the types for this causes way more problems than it solves. + // Omit<…> has issues for instance when spreading the rest of the props on the component. + // @!ts-expect-error static & unmount together are incompatible Contents diff --git a/packages/@headlessui-react/src/utils/render.ts b/packages/@headlessui-react/src/utils/render.ts index f3eda31e10..4106c4b683 100644 --- a/packages/@headlessui-react/src/utils/render.ts +++ b/packages/@headlessui-react/src/utils/render.ts @@ -11,7 +11,7 @@ import { type ReactElement, type Ref, } from 'react' -import type { Expand, Props, XOR, __ } from '../types' +import type { Expand, Props } from '../types' import { classNames } from './class-names' import { match } from './match' @@ -40,17 +40,21 @@ export enum RenderStrategy { Hidden, } +type UnionToIntersection = (T extends any ? (x: T) => any : never) extends (x: infer R) => any + ? R + : never + type PropsForFeature< TPassedInFeatures extends RenderFeatures, TForFeature extends RenderFeatures, TProps, -> = { - [P in TPassedInFeatures]: P extends TForFeature ? TProps : __ -}[TPassedInFeatures] +> = TPassedInFeatures extends TForFeature ? TProps : {} -export type PropsForFeatures = XOR< - PropsForFeature, - PropsForFeature +export type PropsForFeatures = Expand< + UnionToIntersection< + | PropsForFeature + | PropsForFeature + > > export function render({