Skip to content

[labs/react] better union / intersection of ReactComponent Props and Web Component attributes - #3072

Merged
taylor-vann merged 11 commits into
mainfrom
labs_react__faster_types
Jul 13, 2022
Merged

taylor-vann merged 11 commits into
mainfrom
labs_react__faster_types

Conversation

@taylor-vann

@taylor-vann taylor-vann commented Jun 21, 2022 •

Copy link
Copy Markdown
Contributor

Currently Typescript struggles to react to changes in a promptly manner.

This is probably due to a couple of nested structures bogging down the type checker.

Replacing the following:

  type UserProps = React.PropsWithChildren<
    React.PropsWithRef<
      Partial<Omit<I, 'children'>> &
        Partial<EventProps<E>> &
        Omit<React.HTMLAttributes<HTMLElement>, keyof E>
    >

With the following set of types

  type ReactProps = Omit<React.HTMLAttributes<I>, keyof E>;
  type ElementWithoutPropsOrEvents = Omit<I, keyof E | keyof ReactProps>;
  type UserProps = Partial<
    ElementWithoutChildrenOrEvents & ReactProps & EventProps<E>
  >;

Helps typescript inference types much faster.

It's also a better intersection of props and attributes:
a) all react component props without event keys
b) all element properties without react component props or event keys
c) (a) & (b) & (event keys)

@changeset-bot

changeset-bot Bot commented Jun 21, 2022 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7154804

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@lit-labs/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@taylor-vann
taylor-vann marked this pull request as ready for review June 21, 2022 22:22
@github-actions

github-actions Bot commented Jun 21, 2022 •

Copy link
Copy Markdown
Contributor

📊 Tachometer Benchmark Results

Summary

nop-update

  • lit-html-kitchen-sink: unsure 🔍 -2% - +11% (-0.54ms - +3.21ms)
    this-change vs tip-of-tree

render

  • lit-element-list: 83.61ms - 85.47ms
  • lit-html-kitchen-sink: unsure 🔍 -2% - +2% (-0.60ms - +0.52ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -1% - +4% (-0.16ms - +0.43ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -1% - +2% (-0.87ms - +0.96ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -3% - +0% (-2.13ms - +0.28ms)
    this-change vs tip-of-tree

update

  • lit-element-list: 852.58ms - 869.88ms
  • lit-html-kitchen-sink: unsure 🔍 -5% - +4% (-4.94ms - +3.79ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -1% - +2% (-4.59ms - +6.34ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -1% - +2% (-1.19ms - +2.78ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -0% - +1% (-3.80ms - +13.12ms)
    this-change vs tip-of-tree

update-reflect

  • lit-element-list: 864.34ms - 882.35ms
  • reactive-element-list: unsure 🔍 -3% - +1% (-34.41ms - +10.73ms)
    this-change vs tip-of-tree

Results

lit-element-list

render

VersionAvg timevs
91.24ms - 93.29ms-

update

VersionAvg timevs
966.97ms - 975.45ms-

update-reflect

VersionAvg timevs
981.31ms - 990.65ms-
lit-html-kitchen-sink

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
33.71ms - 34.55ms-unsure 🔍
-2% - +2%
-0.60ms - +0.52ms
unsure 🔍
-4% - +1%
-1.23ms - +0.32ms
tip-of-tree
tip-of-tree
33.80ms - 34.53msunsure 🔍
-2% - +2%
-0.52ms - +0.60ms
-unsure 🔍
-3% - +1%
-1.16ms - +0.33ms
previous-release
previous-release
33.93ms - 35.23msunsure 🔍
-1% - +4%
-0.32ms - +1.23ms
unsure 🔍
-1% - +3%
-0.33ms - +1.16ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
92.73ms - 98.78ms-unsure 🔍
-5% - +4%
-4.94ms - +3.79ms
unsure 🔍
-7% - +2%
-7.37ms - +1.69ms
tip-of-tree
tip-of-tree
93.17ms - 99.48msunsure 🔍
-4% - +5%
-3.79ms - +4.94ms
-unsure 🔍
-7% - +2%
-6.89ms - +2.35ms
previous-release
previous-release
95.22ms - 101.97msunsure 🔍
-2% - +8%
-1.69ms - +7.37ms
unsure 🔍
-2% - +7%
-2.35ms - +6.89ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
29.75ms - 33.40ms-unsure 🔍
-2% - +11%
-0.54ms - +3.21ms
unsure 🔍
-3% - +10%
-0.86ms - +2.99ms
tip-of-tree
tip-of-tree
29.80ms - 30.68msunsure 🔍
-10% - +1%
-3.21ms - +0.54ms
-unsure 🔍
-3% - +2%
-1.03ms - +0.49ms
previous-release
previous-release
29.89ms - 31.13msunsure 🔍
-9% - +3%
-2.99ms - +0.86ms
unsure 🔍
-2% - +3%
-0.49ms - +1.03ms
-
lit-html-repeat

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
11.73ms - 12.11ms-unsure 🔍
-1% - +4%
-0.16ms - +0.43ms
slower ❌
1% - 5%
0.06ms - 0.55ms
tip-of-tree
tip-of-tree
11.56ms - 12.01msunsure 🔍
-4% - +1%
-0.43ms - +0.16ms
-unsure 🔍
-1% - +4%
-0.11ms - +0.45ms
previous-release
previous-release
11.46ms - 11.77msfaster ✔
1% - 5%
0.06ms - 0.55ms
unsure 🔍
-4% - +1%
-0.45ms - +0.11ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
334.65ms - 342.92ms-unsure 🔍
-1% - +2%
-4.59ms - +6.34ms
unsure 🔍
-1% - +2%
-4.66ms - +6.75ms
tip-of-tree
tip-of-tree
334.34ms - 341.48msunsure 🔍
-2% - +1%
-6.34ms - +4.59ms
-unsure 🔍
-2% - +2%
-5.15ms - +5.48ms
previous-release
previous-release
333.81ms - 341.68msunsure 🔍
-2% - +1%
-6.75ms - +4.66ms
unsure 🔍
-2% - +2%
-5.48ms - +5.15ms
-
lit-html-template-heavy

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
59.58ms - 60.98ms-unsure 🔍
-1% - +2%
-0.87ms - +0.96ms
unsure 🔍
-1% - +3%
-0.54ms - +1.51ms
tip-of-tree
tip-of-tree
59.65ms - 60.83msunsure 🔍
-2% - +1%
-0.96ms - +0.87ms
-unsure 🔍
-1% - +2%
-0.51ms - +1.39ms
previous-release
previous-release
59.05ms - 60.54msunsure 🔍
-2% - +1%
-1.51ms - +0.54ms
unsure 🔍
-2% - +1%
-1.39ms - +0.51ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
128.58ms - 131.59ms-unsure 🔍
-1% - +2%
-1.19ms - +2.78ms
unsure 🔍
-1% - +3%
-1.08ms - +3.29ms
tip-of-tree
tip-of-tree
128.00ms - 130.59msunsure 🔍
-2% - +1%
-2.78ms - +1.19ms
-unsure 🔍
-1% - +2%
-1.73ms - +2.36ms
previous-release
previous-release
127.40ms - 130.56msunsure 🔍
-3% - +1%
-3.29ms - +1.08ms
unsure 🔍
-2% - +1%
-2.36ms - +1.73ms
-
reactive-element-list

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
64.93ms - 66.81ms-unsure 🔍
-4% - +0%
-2.41ms - +0.10ms
unsure 🔍
-4% - +0%
-2.52ms - +0.14ms
tip-of-tree
tip-of-tree
66.19ms - 67.85msunsure 🔍
-0% - +4%
-0.10ms - +2.41ms
-unsure 🔍
-2% - +2%
-1.29ms - +1.22ms
previous-release
previous-release
66.11ms - 68.00msunsure 🔍
-0% - +4%
-0.14ms - +2.52ms
unsure 🔍
-2% - +2%
-1.22ms - +1.29ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
980.43ms - 995.01ms-unsure 🔍
-1% - +1%
-11.46ms - +9.82ms
unsure 🔍
-1% - +1%
-13.03ms - +8.18ms
tip-of-tree
tip-of-tree
980.78ms - 996.29msunsure 🔍
-1% - +1%
-9.82ms - +11.46ms
-unsure 🔍
-1% - +1%
-12.53ms - +9.32ms
previous-release
previous-release
982.44ms - 997.84msunsure 🔍
-1% - +1%
-8.18ms - +13.03ms
unsure 🔍
-1% - +1%
-9.32ms - +12.53ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
1072.03ms - 1104.54ms-unsure 🔍
-2% - +2%
-25.11ms - +19.84ms
unsure 🔍
-3% - +1%
-29.91ms - +13.50ms
tip-of-tree
tip-of-tree
1075.40ms - 1106.45msunsure 🔍
-2% - +2%
-19.84ms - +25.11ms
-unsure 🔍
-2% - +1%
-26.73ms - +15.60ms
previous-release
previous-release
1082.10ms - 1110.87msunsure 🔍
-1% - +3%
-13.50ms - +29.91ms
unsure 🔍
-1% - +2%
-15.60ms - +26.73ms
-

tachometer-reporter-action v2 for Benchmarks

@taylor-vann

taylor-vann commented Jun 21, 2022 •

Copy link
Copy Markdown
Contributor Author

This will resolve: #3021

could affect: #3048

@taylor-vann
taylor-vann requested a review from sorvell June 21, 2022 23:22
@taylor-vann

Copy link
Copy Markdown
Contributor Author

This is a better to union and intersect React Props, Events, and Element properties.

a) get react props without event keys
b) get element without react props or event keys
c) combine (a) & (b) & Events

@sorvell sorvell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a better explanation of the change here. For example, can you explicate the various statements in the old v. new code, explain why they are equivalent, and then explain why the change is better. Thanks.

@taylor-vann

taylor-vann commented Jun 28, 2022 •

Copy link
Copy Markdown
Contributor Author

No problem @sorvell :)

These changes improve our typing in a couple of ways.

before:

  type UserProps = React.PropsWithChildren<
    React.PropsWithRef<
      Partial<Omit<I, 'children'>> &
        Partial<EventProps<E>> &
        Omit<React.HTMLAttributes<HTMLElement>, keyof E>
    >

after:

  type ReactProps = Omit<React.HTMLAttributes<I>, keyof E>;
  type ElementWithoutPropsOrEvents = Omit<I, keyof E | keyof ReactProps>;
  type UserProps = Partial<
    ElementWithoutChildrenOrEvents & ReactProps & EventProps<E>
  >;

a) Removes the need to explicitly maintain react specific properties like 'style' or 'children' or 'className'. Mainly because react provides these types out of the box through React.HTMLAttributes.

    interface DOMAttributes<T> {
        children?: ReactNode | undefined;
        ...
    }
    
    interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
        ...
        className?: string | undefined;
        ...
        style?: CSSProperties | undefined;
        ...
    }

b) Likewise, these changes remove redundant or duplicate key:value statements. For instance, 'children' is provided by both React.PropsWithChildren and React.HTMLAttributes. So the following two types are the same:

type DemoComponent = React.HTMLAttributes<I>;
type DemoComponent2 = React.PropsWithChildren<React.HTMLAttributes<I>>;

c) Unions on multiple partials is the same as partial-ing a union. It also reads much better. As in the following two types are the same:

type UnionPartial = Partial<I> & Partial<E>;
type UnionPartial = Partial<I & E>;

d) Types are built around Event properties. Currently we use nested React types and a sprinkle of omitted properties to get the types we need. However I think taking a more categorical approach makes what we're exposing to clients through UserProps much more clear.

Currently the logic reads in one big chunk:

inside ReactPropsWithChildren
  inside ReactPropsWithRef ->
    Union partial of HTMLElement without children prop
      with a partial of EventProps
      with React.HTMLElements without EventProps keys


  type UserProps = React.PropsWithChildren<
    React.PropsWithRef<
      Partial<Omit<I, 'children'>> &
        Partial<EventProps<E>> &
        Omit<React.HTMLAttributes<HTMLElement>, keyof E>
    >

These changes create types more correctly by using intersections and unions. It also incrementally defines what types are needed rather than nesting:

ReactProps from HTMLElement without EventProps keys (missing from above)
Element attributes without EventProps keys or ReactProps keys
Union (ReactProps without EventProp keys) and (Element without ReactProp or EventProps keys) with EventProps.

  type ReactProps = Omit<React.HTMLAttributes<I>, keyof E>;
  type ElementWithoutPropsOrEvents = Omit<I, keyof E | keyof ReactProps>;
  type UserProps = Partial<
    ElementWithoutChildrenOrEvents & ReactProps & EventProps<E>
  >;

e) All components have a ref property. There is no need for React.PropsWithRef.

f) there is a notice-able change in lint-ing speed in vscode from these changes, however the actual build-speed changes are negligible.

@taylor-vann taylor-vann changed the title [labs/react] flatter faster types for UserProps [labs/react] better union / intersection of ReactComponent Props and Web Component attributes Jul 11, 2022
@taylor-vann
taylor-vann requested a review from sorvell July 11, 2022 23:26
@taylor-vann
taylor-vann dismissed sorvell’s stale review July 11, 2022 23:27

:) stale-ness

Comment thread .changeset/calm-bottles-laugh.md Outdated
@@ -0,0 +1,5 @@
---
'@lit-labs/react': major

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a major change, yeah?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. This is not a breaking change but it could affect typings downstream.

I wanna say this is a patch since it doesn't break anything.

@taylor-vann

Copy link
Copy Markdown
Contributor Author

Gonna confirm ref before merge :)

@taylor-vann

Copy link
Copy Markdown
Contributor Author

Proof of refs already in a DOMElement in react :)

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L162-L165
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L203-L204

// string fallback for custom web-components
interface DOMElement<P extends HTMLAttributes<T> | SVGAttributes<T>, T extends Element> extends ReactElement<P, string> {
    ref: LegacyRef<T>;
}
...
type DOMFactory<P extends DOMAttributes<T>, T extends Element> =
    (props?: ClassAttributes<T> & P | null, ...children: ReactNode[]) => DOMElement<P, T>;

Quite confident we should be fine without explicitly stating the ref property.

Also adding the React.PropsWithRef should be pretty quick if stuff goes bad!

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.

3 participants