Repository navigation
[labs/react] better union / intersection of ReactComponent Props and Web Component attributes - #3072
Conversation
🦋 Changeset detectedLatest commit: 7154804 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
📊 Tachometer Benchmark ResultsSummarynop-update
render
update
update-reflect
Resultslit-element-list
render
update
update-reflect
lit-html-kitchen-sink
render
update
nop-update
lit-html-repeat
render
update
lit-html-template-heavy
render
update
reactive-element-list
render
update
update-reflect
|
|
This is a better to union and intersect React Props, Events, and Element properties. a) get react props without event keys |
sorvell
left a comment
There was a problem hiding this comment.
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.
|
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 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 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 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 f) there is a notice-able change in lint-ing speed in vscode from these changes, however the actual build-speed changes are negligible. |
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| '@lit-labs/react': major | |||
There was a problem hiding this comment.
This is not a major change, yeah?
There was a problem hiding this comment.
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.
|
Gonna confirm ref before merge :) |
|
Proof of refs already in a https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L162-L165 // 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 Also adding the |
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:
With the following set of types
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)