-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(compose): update types to provide better infers #13841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -5,7 +5,7 @@ import { wasComposedPreviously } from './wasComposedPreviously'; | |||
import { mergeComposeOptions } from './mergeComposeOptions'; | |||
|
|||
function compose< | |||
TElementType extends React.ElementType, | |||
TElementType extends keyof JSX.IntrinsicElements, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TElementType
should be always an HTML tag as refs and props will be inferred from it.
E extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any> | ComponentWithAs | ||
> = E extends { __PRIVATE_PROPS: any } | ||
? E['__PRIVATE_PROPS'] | ||
: JSX.LibraryManagedAttributes<E, React.ComponentPropsWithRef<E>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a hack for ComponentWithAs
as otherwise it will be resolved to:
(props: Record<any, any> & { as?: React.ElementType } & SomeProps) => JSX.Element
As TS struggles to resolve TExtendedElementType
.
The idea of __PRIVATE_PROPS
is based on the assumption that it's possible to use as
only once:
<Button /> => // button
<Button as='div' /> => // div
<Button as={Link} /> => // a
// ??? No way to pass `as` for `Link` 👍
In this case we can assign __PRIVATE_PROPS
to a default value of TElementType
to simplify the resolutions.
displayName?: string; | ||
|
||
defaultProps?: Partial<TProps & { as: TElementType }>; | ||
export type ComponentWithAs<TElementType extends keyof JSX.IntrinsicElements = 'div', TProps = {}> = (< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted this thing to be case because of strange resolutions of TS: microsoft/TypeScript#39313
ComponentWithAs
is good enough to be used of other our components even without compose()
, but before this change it marked props
as any
:
const Divider: ComponentWithAs<'div', DividerProps> = (props /* becomes any 💣 */) => {}
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit aa2ee99:
|
Asset size changesSize Auditor did not detect a change in bundle size for any component! Baseline commit: 50a3aaef8b7b967078b850656354594fee30bbbd (build) |
…/microsoft/fluentui into fix/compose-types � Conflicts: � packages/fluentui/react-northstar/test/specs/commonTests/isConformant.tsx
Perf AnalysisNo significant results to display. All results
Perf Analysis (Fluent)Potential regressions comparing to master
Perf comparison
Perf tests with no regressions
|
🎉 Handy links: |
Pull request checklist
$ yarn change
Description of changes
See comments on code inside the PR.