Skip to content

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

Merged
merged 7 commits into from
Jun 29, 2020

Conversation

layershifter
Copy link
Member

Pull request checklist

  • Addresses an existing issue: Fixes #0000
  • Include a change request file using $ yarn change

Description of changes

See comments on code inside the PR.

@@ -5,7 +5,7 @@ import { wasComposedPreviously } from './wasComposedPreviously';
import { mergeComposeOptions } from './mergeComposeOptions';

function compose<
TElementType extends React.ElementType,
TElementType extends keyof JSX.IntrinsicElements,
Copy link
Member Author

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>>;
Copy link
Member Author

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 = {}> = (<
Copy link
Member Author

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 💣 */) => {}

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 29, 2020

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:

Sandbox Source
Fluent UI Button Configuration

@size-auditor
Copy link

size-auditor bot commented Jun 29, 2020

Asset size changes

Size 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
@DustyTheBot
Copy link

DustyTheBot commented Jun 29, 2020

Warnings
⚠️ There are no updates provided to CHANGELOG. Ensure there are no publicly visible changes introduced by this PR.

Generated by 🚫 dangerJS against aa2ee99

@msft-github-bot
Copy link
Contributor

msft-github-bot commented Jun 29, 2020

Perf Analysis

No significant results to display.

All results

Scenario Render type Master Ticks PR Ticks Iterations Status
BaseButton mount 904 949 5000
ButtonNext mount 553 531 5000
Checkbox mount 1634 1620 5000
CheckboxBase mount 1337 1385 5000
CheckboxNext mount 1642 1560 5000
ChoiceGroup mount 5254 5240 5000
ComboBox mount 965 989 1000
CommandBar mount 8026 7936 1000
ContextualMenu mount 13695 14511 1000
DefaultButton mount 1149 1151 5000
DetailsRow mount 3650 3669 5000
DetailsRowFast mount 3717 3740 5000
DetailsRowNoStyles mount 3442 3435 5000
Dialog mount 1522 1584 1000
DocumentCardTitle mount 1867 1901 1000
Dropdown mount 2631 2587 5000
FocusZone mount 1788 1910 5000
IconButton mount 1805 1866 5000
Label mount 374 370 5000
Link mount 460 479 5000
LinkNext mount 495 505 5000
MenuButton mount 1855 1472 5000
Nav mount 3398 3425 1000
Panel mount 1488 1497 1000
Persona mount 871 867 1000
Pivot mount 1499 1459 1000
PivotNext mount 1438 1417 1000
PrimaryButton mount 1317 1290 5000
SearchBox mount 1292 1309 5000
SearchBoxNext mount 1431 1348 5000
Slider mount 1582 1570 5000
SliderNext mount 2124 2097 5000
Spinner mount 446 445 5000
SplitButton mount 3266 3274 5000
Stack mount 543 531 5000
StackWithIntrinsicChildren mount 1983 2069 5000
StackWithTextChildren mount 5172 5274 5000
TagPicker mount 2921 2855 5000
Text mount 432 435 5000
TextField mount 1505 1487 5000
ThemeProvider mount 3019 3143 5000
ThemeProvider virtual-rerender 525 506 5000
Toggle mount 856 871 5000
ToggleNext mount 846 854 5000
button mount 120 123 5000

Perf Analysis (Fluent)

⚠️ 1 potential perf regressions detected

Potential regressions comparing to master

Scenario Current PR Ticks Baseline Ticks Ratio Regression Analysis
FlexMinimalPerf.default 291 277 1.05:1 analysis
Perf comparison
Status Scenario Fluent TPI Fabric TPI Ratio Iterations Ticks
🎯 Avatar.Fluent 0.42 0.51 0.82:1 2000 832
🦄 Button.Fluent 0.11 0.2 0.55:1 5000 538
🔧 Checkbox.Fluent 0.63 0.34 1.85:1 1000 631
🦄 Dialog.Fluent 0.15 0.22 0.68:1 5000 752
🔧 Dropdown.Fluent 3.33 0.46 7.24:1 1000 3331
🔧 Icon.Fluent 0.14 0.05 2.8:1 5000 682
🦄 Image.Fluent 0.07 0.11 0.64:1 5000 351
🔧 Slider.Fluent 1.72 0.37 4.65:1 1000 1718
🔧 Text.Fluent 0.06 0.03 2:1 5000 299
🦄 Tooltip.Fluent 0.1 18.44 0.01:1 5000 511

🔧 Needs work     🎯 On target     🦄 Amazing

Perf tests with no regressions
Scenario Current PR Ticks Baseline Ticks Ratio
ListMinimalPerf.default 492 445 1.11:1
AvatarMinimalPerf.default 469 425 1.1:1
VideoMinimalPerf.default 632 582 1.09:1
AttachmentSlotsPerf.default 1182 1093 1.08:1
BoxMinimalPerf.default 349 327 1.07:1
ImageMinimalPerf.default 356 335 1.06:1
TextMinimalPerf.default 345 327 1.06:1
TreeWith60ListItems.default 222 210 1.06:1
GridMinimalPerf.default 684 652 1.05:1
HeaderMinimalPerf.default 365 349 1.05:1
InputMinimalPerf.default 1088 1040 1.05:1
SliderMinimalPerf.default 1627 1555 1.05:1
TooltipMinimalPerf.default 768 730 1.05:1
Slider.Fluent 1718 1632 1.05:1
RadioGroupMinimalPerf.default 401 386 1.04:1
DropdownMinimalPerf.default 3483 3372 1.03:1
ListCommonPerf.default 964 939 1.03:1
ListWith60ListItems.default 1102 1073 1.03:1
Dialog.Fluent 752 729 1.03:1
AttachmentMinimalPerf.default 149 146 1.02:1
IconMinimalPerf.default 634 621 1.02:1
TextAreaMinimalPerf.default 445 437 1.02:1
CustomToolbarPrototype.default 3766 3708 1.02:1
AccordionMinimalPerf.default 145 143 1.01:1
ButtonSlotsPerf.default 620 615 1.01:1
CarouselMinimalPerf.default 448 443 1.01:1
DialogMinimalPerf.default 746 741 1.01:1
HierarchicalTreeMinimalPerf.default 409 403 1.01:1
ItemLayoutMinimalPerf.default 1229 1217 1.01:1
LabelMinimalPerf.default 392 389 1.01:1
RefMinimalPerf.default 219 216 1.01:1
TreeMinimalPerf.default 856 844 1.01:1
Dropdown.Fluent 3331 3314 1.01:1
Icon.Fluent 682 673 1.01:1
ChatMinimalPerf.default 591 589 1:1
HeaderSlotsPerf.default 796 794 1:1
MenuMinimalPerf.default 843 841 1:1
ProviderMergeThemesPerf.default 1964 1970 1:1
SplitButtonMinimalPerf.default 3729 3737 1:1
Button.Fluent 538 536 1:1
Checkbox.Fluent 631 631 1:1
Image.Fluent 351 350 1:1
Tooltip.Fluent 511 513 1:1
ButtonMinimalPerf.default 165 167 0.99:1
ChatDuplicateMessagesPerf.default 433 437 0.99:1
DividerMinimalPerf.default 329 333 0.99:1
EmbedMinimalPerf.default 1865 1887 0.99:1
MenuButtonMinimalPerf.default 1516 1535 0.99:1
ProviderMinimalPerf.default 857 867 0.99:1
ReactionMinimalPerf.default 384 386 0.99:1
SegmentMinimalPerf.default 320 323 0.99:1
AnimationMinimalPerf.default 384 390 0.98:1
CardMinimalPerf.default 533 545 0.98:1
FormMinimalPerf.default 375 383 0.98:1
StatusMinimalPerf.default 665 679 0.98:1
TableManyItemsPerf.default 2172 2207 0.98:1
ToolbarMinimalPerf.default 931 952 0.98:1
Avatar.Fluent 832 853 0.98:1
DropdownManyItemsPerf.default 1410 1450 0.97:1
ListNestedPerf.default 884 913 0.97:1
PopupMinimalPerf.default 648 667 0.97:1
PortalMinimalPerf.default 116 119 0.97:1
ChatWithPopoverPerf.default 447 466 0.96:1
LayoutMinimalPerf.default 375 392 0.96:1
AlertMinimalPerf.default 270 284 0.95:1
CheckboxMinimalPerf.default 2823 2981 0.95:1
TableMinimalPerf.default 376 405 0.93:1
Text.Fluent 299 324 0.92:1
LoaderMinimalPerf.default 701 767 0.91:1

@layershifter layershifter merged commit 97c2a14 into master Jun 29, 2020
@layershifter layershifter deleted the fix/compose-types branch June 29, 2020 18:51
@msft-github-bot
Copy link
Contributor

🎉@fluentui/[email protected] has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants