Skip to content

Commit

Permalink
fix: Fix unions generating invalid prop types.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 28, 2020
1 parent 12aea4c commit 172092d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/convertBabelToPropTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ function convert(type: any, state: ConvertState, depth: number): PropType | null
} else if (t.isTSUnionType(type) || t.isTSIntersectionType(type)) {
const isAllLiterals = type.types.every(param => t.isTSLiteralType(param));
const containsAny = type.types.some(param => t.isTSAnyKeyword(param));
const containsNull = type.types.some(param => t.isTSNullKeyword(param));
let label;
let args;

Expand All @@ -267,7 +266,7 @@ function convert(type: any, state: ConvertState, depth: number): PropType | null
label = t.identifier('oneOfType');

// Contained unresolved references, so just omit for now
if (args.length !== type.types.length && args.length === 1 && (containsAny || containsNull)) {
if (args.length !== type.types.length) {
return null;
}
}
Expand Down
10 changes: 2 additions & 8 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1444,17 +1444,11 @@ export interface Props {
export default class NullUndefined extends React.Component<Props> {
static propTypes = {
nullable: _pt.oneOfType([_pt.string, _pt.oneOf([null])]),
undefined: _pt.oneOfType([_pt.string]),
optional: _pt.string,
optionalUndefined: _pt.oneOfType([_pt.string]),
optionalNullable: _pt.oneOfType([_pt.string, _pt.oneOf([null])]),
optionalBoth: _pt.oneOfType([_pt.string, _pt.oneOf([null])]),
literalNullable: _pt.oneOfType([_pt.oneOf(['foo']), _pt.oneOf([null])]),
literalUndefined: _pt.oneOfType([_pt.oneOf(['foo'])]),
literalOptional: _pt.oneOf(['foo']),
literalOptionalUndefined: _pt.oneOfType([_pt.oneOf(['foo'])]),
literalOptionalNullable: _pt.oneOfType([_pt.oneOf(['foo']), _pt.oneOf([null])]),
literalOptionalBoth: _pt.oneOfType([_pt.oneOf(['foo']), _pt.oneOf([null])])
literalOptionalNullable: _pt.oneOfType([_pt.oneOf(['foo']), _pt.oneOf([null])])
};
render() {
Expand Down Expand Up @@ -1640,7 +1634,7 @@ export interface Props {
typeRefIntersection?: Foo & Bar;
enumUnion: Color.RED | Color.BLUE;
enumIntersection: Color.BLUE & Color.GREEN;
missingToNull?: InvalidReference | null;
missingRefs?: InvalidReference | null;
}
export default class TypeUnionIntersection extends React.Component<Props> {
static propTypes = {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/type-unions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Props {
enumUnion: Color.RED | Color.BLUE;
enumIntersection: Color.BLUE & Color.GREEN;
// @ts-ignore
missingToNull?: InvalidReference | null;
missingRefs?: InvalidReference | null;
}

export default class TypeUnionIntersection extends React.Component<Props> {
Expand Down

0 comments on commit 172092d

Please sign in to comment.