-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PropsOf<C> TypeScript utilities - Support defaultProps #1405
PropsOf<C> TypeScript utilities - Support defaultProps #1405
Conversation
…to new JSX and React types.
🦋 Changeset is good to goLatest commit: 30707a0 We got this. Not sure what this means? Click here to learn what changesets are. |
…ore and root package versions.
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.
Superb work! Love such PRs ❤️
Thank you @Andarist and @mitchellhamilton for the quick turn-around on this. @ahutchings and I spent some extra time in making the PR comprehensive hoping it would be easier to review and accept. Superb work on emotion as well! Love such libraries ❤️ |
@Andarist - Do you mind sharing when you expect this to make it into a release and what semver level it will likely be? |
Not sure, @mitchellhamilton is doing releases - usually, it doesn't take too much time for a new release to be prepared though. I think it should qualify as patch/minor - we definitely don't consider typing changes to affect semver in the same way as runtime changes affect it. |
Great! Thank you |
* PropsOf<C> TypeScript utilities - Support defaultProps by delegating to new JSX and React types. * changeset * Regenerated yarn.lock without internal urls * Align versions of @types/react across project by upgrading @emotion/core and root package versions.
What:
Upgrade the
PropsOf<C>
TypeScript utilities to supportdefaultProps
by delegating to new JSX and React types.Why:
The
PropsOf<C>
utilities in @emotion/styled-base and emotion/theming currently result in a TypeScript error when usingstyled
orwithTheme
with a component that has adefaultProp
that is made optional by defaulting the prop value, yet is required on the Props interface and no value is passed to the component instance.See tests in this PR for both class-based and function-based component scenarios.
How:
The solution provided here uses some newer typings for JSX and React (requiring some TypeScript and @types/react upgrades).
Complexity is reduced by adapting a library-provided interface that is parameterized by both a component and its props (
JSX.LibraryManagedAttributes<C, P>
) to our utility interface parameterized by only a component (PropsOf<C>
).The
React.ComponentProps<C>
type utility is the key that allows us to extract Props (P) from the component (C).The
JSX.LibraryManagedAttributes<C, P>
type utility ensuresprops
with associateddefaultProps
are made optional (also seeReactManagedAttributes
andDefaultize
utilities in @types/react).The type parameter
C
is constrained to match that ofReact.ComponentProps<C>
:keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
.A primary benefit of adapting to standard interfaces is reduced type maintenance over time. I haven't tested it, but also noticed that Issue #991 may be resolved by this change as well since union prop types should be supported by these standard interfaces.
Note: Upon running
yarn test:typescript
with a clean build from master, I encountered TypeScript errors related toReact.ReactNode
's use of a union with both null and undefined included. I have suppressed the error by turning off theno-null-undefined-union
flag in tslint; however, you may prefer a different approach.Checklist: