From aef30ea878fc451b5a130735d310f7dcbae84118 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Fri, 4 Dec 2026 19:20:51 +0900 Subject: [PATCH] perf(react): type JSX children so the default overload matches them as a subtype With more than one overload, the checker first looks for a signature in a subtype pass. A JSX element is a ReactElement, and `any` is assignable to ReactNode but not its subtype, so every Box, Flex, Text... with element children failed the subtype pass on its default overload, and the checker inferred and checked the polymorphic `as` overload and the trailing default before it took the first one in the assignability pass. The default props now type `children` as `C | JSX.Element | readonly (...)[]`, used only where `C` accepts every value of that union, so the props accept and reject the same values while JSX children match as a subtype. Large benchmark app, 20 alternating runs, medians: tsc check 107 -> 82 ms, Next's TypeScript phase 474 -> 412 ms; types 66,143 -> 36,187. Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_jsx_children_subtype.json | 7 ++ packages/react/src/types/props/index.ts | 32 ++++++- packages/react/type-tests/default-props.tsx | 95 +++++++++++++++++++ packages/react/type-tests/native/app.tsx | 51 ++++++++++ packages/react/type-tests/same-props.ts | 58 +++++++++++ 5 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 .changepacks/changepack_log_jsx_children_subtype.json create mode 100644 packages/react/type-tests/default-props.tsx create mode 100644 packages/react/type-tests/same-props.ts diff --git a/.changepacks/changepack_log_jsx_children_subtype.json b/.changepacks/changepack_log_jsx_children_subtype.json new file mode 100644 index 000000000..a795756ff --- /dev/null +++ b/.changepacks/changepack_log_jsx_children_subtype.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/react/package.json": "Patch" + }, + "note": "Type-check JSX faster: the props `Box`, `Flex`, `Text` and the other components take for their default element type `children` so JSX children match it as a subtype, so the checker no longer infers and checks the polymorphic `as` overload of every element with element children before taking the default one. The props accept and reject the same values. On the large benchmark app `tsc` checks in 82 ms instead of 107 ms, and Next's TypeScript phase takes 412 ms instead of 474 ms (medians of 20 alternating runs).", + "date": "2026-12-04T00:00:00.000Z" +} diff --git a/packages/react/src/types/props/index.ts b/packages/react/src/types/props/index.ts index 043bedb09..ad803707a 100644 --- a/packages/react/src/types/props/index.ts +++ b/packages/react/src/types/props/index.ts @@ -79,13 +79,39 @@ export type DevupIntrinsicProps = ? DevupElementProps[T] : React.ComponentProps +/** + * What `children` of type `C` takes, written so JSX children match it as a + * subtype: JSX elements, arrays of children, or `C`. A JSX element is a + * `ReactElement`, and `any` is assignable to `ReactNode` but not its + * subtype, so against `ReactNode` alone the subtype pass of overload + * resolution rejects the first overload of every component with element + * children, and the checker goes on to infer and check the polymorphic one + * before it assigns to the first. It is used only where `C` accepts every + * value of the union, so the props take the same values as with `C`. + */ +type DevupJsxChildren = + C | React.JSX.Element | readonly DevupJsxChildren[] + +/** The `children` of the props `P`, typed with `DevupJsxChildren` where that takes the same values */ +type DevupDefaultChildrenProps

= { + [K in keyof P as K extends 'children' ? K : never]: [ + DevupJsxChildren, + ] extends [P[K]] + ? DevupJsxChildren + : P[K] +} + // Component declarations put this overload first for fast resolution and last // so Parameters keeps describing the default element. export type DevupDefaultComponentMergedProps< T extends keyof React.JSX.IntrinsicElements, -> = Omit, DevupDefaultIntrinsicPropConflicts> & { - props?: FilterChildren> -} & DevupComponentProps +> = Omit< + DevupIntrinsicProps, + DevupDefaultIntrinsicPropConflicts | 'children' +> & + DevupDefaultChildrenProps> & { + props?: FilterChildren> + } & DevupComponentProps export type DevupComponentBaseProps = NoInfer extends string diff --git a/packages/react/type-tests/default-props.tsx b/packages/react/type-tests/default-props.tsx new file mode 100644 index 000000000..f6bf48baa --- /dev/null +++ b/packages/react/type-tests/default-props.tsx @@ -0,0 +1,95 @@ +// What a component takes for the element it renders by default is unchanged +// by typing its `children` so JSX children match them as a subtype, which +// spares the checker the polymorphic overload of every element with element +// children +import { Box, Button, Image, Input, Text } from '../src' +import type { DevupDefaultComponentMergedProps } from '../src/types/props' +import type { + SameProps, + WrittenDefaultComponentMergedProps, +} from './same-props' + +type Assert = T + +type _DivPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'div'>, + WrittenDefaultComponentMergedProps<'div'>, + 'children' + > +> +type _SpanPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'span'>, + WrittenDefaultComponentMergedProps<'span'>, + 'children' + > +> +type _ButtonPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'button'>, + WrittenDefaultComponentMergedProps<'button'>, + 'children' + > +> +type _ImgPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'img'>, + WrittenDefaultComponentMergedProps<'img'>, + 'children' + > +> +type _InputPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'input'>, + WrittenDefaultComponentMergedProps<'input'>, + 'children' + > +> + +// The check tells props apart +// @ts-expect-error a key only one has +type _KeysDiffer = Assert> +// @ts-expect-error a key optional in only one +type _OptionalityDiffers = Assert> +// @ts-expect-error a key typed differently +type _TypesDiffer = Assert> +// @ts-expect-error a key in `Loose` taking other values +type _ValuesDiffer = Assert> +type _SameValues = Assert> + +declare const items: string[] +declare const node: React.ReactNode + +export const children = ( + + one + text + {1} + {null} + {undefined} + {false} + {node} + {items.map((item) => ( + {item} + ))} + {[a, [b]]} + + +) +export const one = ( + + one + +) +export const passed = {node} +// @ts-expect-error an object is not a child +export const object = {{ a: 1 }} +// @ts-expect-error a function is not a child of an element +export const render = {() => a} +// @ts-expect-error an array holding an object is not a child +export const objects = {[{ a: 1 }]} +export const image = +export const input = diff --git a/packages/react/type-tests/native/app.tsx b/packages/react/type-tests/native/app.tsx index 118bdb76d..5951934f1 100644 --- a/packages/react/type-tests/native/app.tsx +++ b/packages/react/type-tests/native/app.tsx @@ -2,6 +2,12 @@ // each takes the props of the React Native component it renders, and its // style props as on the web import { Box, Button, css, Flex, Image, Input, Text } from '@devup-ui/react' +import type { DevupDefaultComponentMergedProps } from '@devup-ui/react/types/props' + +import type { + SameProps, + WrittenDefaultComponentMergedProps, +} from '../same-props' export const app = ( {}} testID="card"> @@ -26,3 +32,48 @@ export const app = ( export const link = // @ts-expect-error a TextInput reports its text with onChangeText export const input = {}} /> + +// The props of each component are unchanged by typing `children` so JSX +// children match them as a subtype, also where they are a React Native +// component's +type Assert = T +type _DivPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'div'>, + WrittenDefaultComponentMergedProps<'div'>, + 'children' + > +> +type _SpanPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'span'>, + WrittenDefaultComponentMergedProps<'span'>, + 'children' + > +> +type _ButtonPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'button'>, + WrittenDefaultComponentMergedProps<'button'>, + 'children' + > +> +type _ImgPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'img'>, + WrittenDefaultComponentMergedProps<'img'>, + 'children' + > +> +type _InputPropsStaySame = Assert< + SameProps< + DevupDefaultComponentMergedProps<'input'>, + WrittenDefaultComponentMergedProps<'input'>, + 'children' + > +> + +// A Pressable renders its children from its state +export const pressed = ( + +) diff --git a/packages/react/type-tests/same-props.ts b/packages/react/type-tests/same-props.ts new file mode 100644 index 000000000..49e7699d2 --- /dev/null +++ b/packages/react/type-tests/same-props.ts @@ -0,0 +1,58 @@ +// Checks a props type rewritten to type-check faster describes the same props +// as the type it was written as: the same keys, each optional in both or in +// neither, and each typed identically, or for the keys in `Loose`, taking the +// same values. +import type { + DevupComponentProps, + DevupIntrinsicProps, +} from '../src/types/props' +import type { DevupCustomShorthands } from '../src/types/shorthand' + +type Identical = + (() => V extends X ? 1 : 2) extends () => V extends Y ? 1 : 2 + ? true + : false + +type Equivalent = [X] extends [Y] + ? [Y] extends [X] + ? true + : false + : false + +type OptionalKeys = { + [K in keyof T]-?: object extends Pick ? K : never +}[keyof T] + +/** The keys `A` and `B` both have and type differently */ +type DifferentlyTypedKeys = { + [K in Extract]-?: ( + K extends Loose ? Equivalent : Identical + ) extends true + ? never + : K +}[Extract] + +/** `true` when `A` and `B` describe the same props; otherwise what differs */ +export type SameProps = + Identical extends false + ? { keys: [Exclude, Exclude] } + : Identical, OptionalKeys> extends false + ? { optional: [OptionalKeys, OptionalKeys] } + : [DifferentlyTypedKeys] extends [never] + ? true + : { types: DifferentlyTypedKeys } + +/** `DevupDefaultComponentMergedProps` as it was written before its `children` took JSX children as a subtype */ +export type WrittenDefaultComponentMergedProps< + T extends keyof React.JSX.IntrinsicElements, +> = Omit< + DevupIntrinsicProps, + | 'color' + | 'content' + | 'height' + | 'translate' + | 'width' + | keyof DevupCustomShorthands +> & { + props?: Omit, 'children'> +} & DevupComponentProps