-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[typescript] Fix with* injectors ignoring defaultProps #12673
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5c36cb0
[core] Fix withStyles ignoring defaultProps
eps1lon d6d88bb
[typescript] Fix chore review issues
eps1lon 25b333b
[typescript] Normalize order of InjectedProps and TargetProps
eps1lon a10f121
[typescript] Remove extraneous type constraint in Shared
eps1lon 917f221
[typescript] Removed extraneous Shared type
eps1lon 93da0df
[typescript] Add more test cases for stateless componets
eps1lon 5da60a0
[typescript] Fix false positive consistency check for implicit SFCs
eps1lon 8c83e14
[typescript] Removed unused prop
eps1lon eed5c48
[typescript] Cleanup component tests
eps1lon 859d2d9
[typescript] Fix wrong injected theme type in withStyles
eps1lon a0ffe02
[typescript] Introduce generic prop injector HOC
eps1lon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import { Theme } from './createMuiTheme'; | ||
import { AnyComponent, ConsistentWith, Overwrite } from '..'; | ||
import { PropInjector } from '..'; | ||
|
||
export interface WithTheme { | ||
theme: Theme; | ||
innerRef?: React.Ref<any> | React.RefObject<any>; | ||
} | ||
|
||
export default function withTheme(): <P extends ConsistentWith<P, WithTheme>>( | ||
component: AnyComponent<P & WithTheme>, | ||
) => React.ComponentType<Overwrite<P, Partial<WithTheme>>>; | ||
export default function withTheme(): PropInjector<WithTheme, Partial<WithTheme>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This consistency check isn't working when applied to an SFC:
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.
@pelotom This is a limitation of the
never
type. I cannot assign T to never but a function that takes T to a function that takes never. typescript playgroundIf I type this function explicitly as a
React.SFC
I do get errors but the message is confusing:Property 'children' is missing in type 'ValidationMap<Props>'
.I'm going to verify if this was working before. If we can't resolve this we have to make a decision whether we want to support
defaultProps
or the consistency check for stateless components.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 did indeed work.
I would prefer
defaultProps
support over consistency checks for implicit SFCs. We could always add a section to the typescript guide.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.
Hm, are you 100% sure that we can't support both? I haven't looked at this in enough detail to know... but I agree that
defaultProps
takes precedence if it has to be one or the other.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.
Not with the
never
approach. That's either an issue with ts (although I couldn't really find reports on this) or an actual limitation. I tried using the previous approach with ConsistentWith but either ts inferred{}
as the prop type or I got errors when passing a styled component towithTheme
I probably revisit the react-redux typings since I used the
Matching
from there and see if I can connect inconsistent props with their typings too. That way we get more focus on the issue and maybe get some more help.But as is the issue with #12697 this problem at least has a workaround with explicit typing.
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.
If we can't do a complete job of enforcing consistency maybe we should just remove the constraints completely, as they significantly complicate the expression of the types, and arguably they're of fairly niche value.
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.
@eps1lon as the original author on the react-redux PR, happy to help out if I can. I haven't gotten any reviews in 20 days so it's honestly been a bit hard to iterate/improve on the definition. If there are edge cases it doesn't handle, I can certainly iterate.
I also have my email in my github profile so feel free to reach out that way.
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.
FWIW, is the issue you're dealing with related to this by any chance DefinitelyTyped/DefinitelyTyped#28249?
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.
That issue is probably causing the fail with the explicitly typed SFC. So if that issue is resolved we might not even have a consistency check for explicitly typed SFCs.