Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_jsx_children_subtype.json
Original file line number Diff line number Diff line change
@@ -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"
}
32 changes: 29 additions & 3 deletions packages/react/src/types/props/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,39 @@ export type DevupIntrinsicProps<T extends keyof React.JSX.IntrinsicElements> =
? DevupElementProps[T]
: React.ComponentProps<T>

/**
* 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<any, any>`, 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> =
C | React.JSX.Element | readonly DevupJsxChildren<C>[]

/** The `children` of the props `P`, typed with `DevupJsxChildren` where that takes the same values */
type DevupDefaultChildrenProps<P> = {
[K in keyof P as K extends 'children' ? K : never]: [
DevupJsxChildren<P[K]>,
] extends [P[K]]
? DevupJsxChildren<P[K]>
: P[K]
}

// Component declarations put this overload first for fast resolution and last
// so Parameters<typeof Component> keeps describing the default element.
export type DevupDefaultComponentMergedProps<
T extends keyof React.JSX.IntrinsicElements,
> = Omit<DevupIntrinsicProps<T>, DevupDefaultIntrinsicPropConflicts> & {
props?: FilterChildren<DevupIntrinsicProps<T>>
} & DevupComponentProps<T>
> = Omit<
DevupIntrinsicProps<T>,
DevupDefaultIntrinsicPropConflicts | 'children'
> &
DevupDefaultChildrenProps<DevupIntrinsicProps<T>> & {
props?: FilterChildren<DevupIntrinsicProps<T>>
} & DevupComponentProps<T>

export type DevupComponentBaseProps<T extends React.ElementType> =
NoInfer<T> extends string
Expand Down
95 changes: 95 additions & 0 deletions packages/react/type-tests/default-props.tsx
Original file line number Diff line number Diff line change
@@ -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 extends true> = 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<SameProps<{ a?: 1 }, { a?: 1; b?: 1 }>>
// @ts-expect-error a key optional in only one
type _OptionalityDiffers = Assert<SameProps<{ a?: 1 }, { a: 1 }>>
// @ts-expect-error a key typed differently
type _TypesDiffer = Assert<SameProps<{ a?: 1 }, { a?: 1 | 2 }>>
// @ts-expect-error a key in `Loose` taking other values
type _ValuesDiffer = Assert<SameProps<{ a?: 1 }, { a?: 1 | 2 }, 'a'>>
type _SameValues = Assert<SameProps<{ a?: 1 | 1 }, { a?: 1 }, 'a'>>

declare const items: string[]
declare const node: React.ReactNode

export const children = (
<Box>
<Text>one</Text>
text
{1}
{null}
{undefined}
{false}
{node}
{items.map((item) => (
<Text key={item}>{item}</Text>
))}
{[<Text key="a">a</Text>, [<Text key="b">b</Text>]]}
<Button>
<Text>press</Text>
</Button>
</Box>
)
export const one = (
<Box>
<Text>one</Text>
</Box>
)
export const passed = <Box>{node}</Box>
// @ts-expect-error an object is not a child
export const object = <Box>{{ a: 1 }}</Box>
// @ts-expect-error a function is not a child of an element
export const render = <Box>{() => <Text>a</Text>}</Box>
// @ts-expect-error an array holding an object is not a child
export const objects = <Box>{[{ a: 1 }]}</Box>
export const image = <Image src="/a.png" />
export const input = <Input value="a" />
51 changes: 51 additions & 0 deletions packages/react/type-tests/native/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<Box bg="red" className={css({ p: 1 })} onLayout={() => {}} testID="card">
Expand All @@ -26,3 +32,48 @@ export const app = (
export const link = <Box href="/" />
// @ts-expect-error a TextInput reports its text with onChangeText
export const input = <Input onInput={() => {}} />

// 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 extends true> = 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 = (
<Button>{({ pressed }) => <Text>{pressed ? 'on' : 'off'}</Text>}</Button>
)
58 changes: 58 additions & 0 deletions packages/react/type-tests/same-props.ts
Original file line number Diff line number Diff line change
@@ -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<X, Y> =
(<V>() => V extends X ? 1 : 2) extends <V>() => V extends Y ? 1 : 2
? true
: false

type Equivalent<X, Y> = [X] extends [Y]
? [Y] extends [X]
? true
: false
: false

type OptionalKeys<T> = {
[K in keyof T]-?: object extends Pick<T, K> ? K : never
}[keyof T]

/** The keys `A` and `B` both have and type differently */
type DifferentlyTypedKeys<A, B, Loose extends PropertyKey> = {
[K in Extract<keyof A, keyof B>]-?: (
K extends Loose ? Equivalent<A[K], B[K]> : Identical<A[K], B[K]>
) extends true
? never
: K
}[Extract<keyof A, keyof B>]

/** `true` when `A` and `B` describe the same props; otherwise what differs */
export type SameProps<A, B, Loose extends PropertyKey = never> =
Identical<keyof A, keyof B> extends false
? { keys: [Exclude<keyof A, keyof B>, Exclude<keyof B, keyof A>] }
: Identical<OptionalKeys<A>, OptionalKeys<B>> extends false
? { optional: [OptionalKeys<A>, OptionalKeys<B>] }
: [DifferentlyTypedKeys<A, B, Loose>] extends [never]
? true
: { types: DifferentlyTypedKeys<A, B, Loose> }

/** `DevupDefaultComponentMergedProps<T>` as it was written before its `children` took JSX children as a subtype */
export type WrittenDefaultComponentMergedProps<
T extends keyof React.JSX.IntrinsicElements,
> = Omit<
DevupIntrinsicProps<T>,
| 'color'
| 'content'
| 'height'
| 'translate'
| 'width'
| keyof DevupCustomShorthands
> & {
props?: Omit<DevupIntrinsicProps<T>, 'children'>
} & DevupComponentProps<T>
Loading