Skip to content

Commit

Permalink
fix(react-intl): support union component prop types in injectIntl (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrocat101 committed Feb 7, 2023
1 parent 404aaa3 commit d970127
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/react-intl/src/components/injectIntl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ export type WrappedComponentProps<IntlPropName extends string = 'intl'> = {
[k in IntlPropName]: IntlShape
}

export type WithIntlProps<P> = Omit<P, keyof WrappedComponentProps> & {
/**
* Utility type to help deal with the fact that `Omit` doesn't play well with unions:
* - https://github.com/microsoft/TypeScript/issues/31501
* - https://github.com/microsoft/TypeScript/issues/28339
*
* @example
* DistributedOmit<X | Y, K> --> Omit<X, K> | Omit<Y, K>
*/
export type DistributedOmit<T, K extends PropertyKey> = T extends unknown
? Omit<T, K>
: never

export type WithIntlProps<P> = DistributedOmit<
P,
keyof WrappedComponentProps
> & {
forwardedRef?: React.Ref<any>
}

Expand Down
18 changes: 18 additions & 0 deletions packages/react-intl/tests/unit/react-intl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,23 @@ describe('react-intl', () => {
// This test only need to pass the type checking.
;<Test />
})

it('injectIntl works with union prop types', () => {
type TestProps = {intl: ReactIntl.IntlShape; base: string} & (
| {type: 'a'; text: string}
| {type: 'b'; value: number}
)

class _Test extends React.Component<TestProps> {
render() {
return null
}
}

const Test = ReactIntl.injectIntl(_Test)

// This test only need to pass the type checking.
;<Test base="base" type="a" text="text" />
})
})
})

0 comments on commit d970127

Please sign in to comment.