Skip to content

Commit 3139447

Browse files
committed
Fix handling of props that are unions of intersections
1 parent a86938f commit 3139447

File tree

3 files changed

+497
-3
lines changed

3 files changed

+497
-3
lines changed

src/parsers/componentParser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function squashComponentProps(callSignatures: CallSignature[], context: ParserCo
7979
}
8080

8181
if (propsParameter.type instanceof UnionNode) {
82-
return unwrapUnionType(propsParameter.type);
82+
const ut = unwrapUnionType(propsParameter.type);
83+
return ut;
8384
}
8485

8586
if (propsParameter.type instanceof IntersectionNode) {
@@ -131,10 +132,10 @@ function squashComponentProps(callSignatures: CallSignature[], context: ParserCo
131132
});
132133
}
133134

134-
function unwrapUnionType(type: UnionNode): ObjectNode[] {
135+
function unwrapUnionType(type: UnionNode): (ObjectNode | IntersectionNode)[] {
135136
return type.types
136137
.map((type) => {
137-
if (type instanceof ObjectNode) {
138+
if (type instanceof ObjectNode || type instanceof IntersectionNode) {
138139
return type;
139140
} else if (type instanceof UnionNode) {
140141
return unwrapUnionType(type);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
import * as React from 'react';
3+
4+
// React.forwardRef makes props an intersection of the provided props and RefAttributes.
5+
// In this case, it's a union of intersections.
6+
7+
export const Button = React.forwardRef(function Button(
8+
props: ButtonProps,
9+
forwardedRef: React.ForwardedRef<HTMLButtonElement>,
10+
) {
11+
const { nativeButton = true, ...other } = props;
12+
return <button ref={forwardedRef} {...other} />;
13+
});
14+
15+
type ButtonProps = ButtonNativeProps | ButtonNonNativeProps;
16+
17+
interface ButtonNativeProps extends HTMLButtonProps {
18+
nativeButton?: true;
19+
}
20+
21+
interface ButtonNonNativeProps extends HTMLButtonProps {
22+
nativeButton: false;
23+
}
24+
25+
interface HTMLButtonProps {
26+
type?: 'button' | 'submit' | 'reset';
27+
id?: string;
28+
className?: string;
29+
}

0 commit comments

Comments
 (0)