-
Notifications
You must be signed in to change notification settings - Fork 269
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
fix(props): add mergeProps utility to resolve all defaultProps warnings #2581
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,7 +1,33 @@ | ||
export function mergeProps<T, U>( | ||
defaultProps: T, | ||
props: U | ||
): { [Key in Extract<keyof T, keyof U>]: U[Key] } & | ||
{ [Key in Exclude<keyof U, keyof T>]?: U[Key] } { | ||
return { ...defaultProps, ...props } | ||
export function mergeProps<A, B>(a: A, b: B): B & A | ||
export function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A | ||
export function mergeProps<A, B, C, D>(a: A, b: B, c: C, d: D): D & C & B & A | ||
export function mergeProps(...items: any[]) { | ||
const ret: any = {} | ||
items.forEach((item) => { | ||
if (item) { | ||
Object.keys(item).forEach((key) => { | ||
if (item[key] !== undefined) { | ||
ret[key] = item[key] | ||
} | ||
}) | ||
} | ||
}) | ||
return ret | ||
} | ||
|
||
/** | ||
* Merge props and return the first non-undefined value. | ||
* The later has higher priority. e.g. (10, 1, 5) => 5 wins. | ||
* This is useful with legacy props that have been deprecated. | ||
*/ | ||
export function mergeProp<T, DefaultT extends T = T>( | ||
defaultProp: DefaultT, | ||
...propList: T[] | ||
): T | undefined { | ||
for (let i = propList.length - 1; i >= 0; i -= 1) { | ||
if (propList[i] !== undefined) { | ||
return propList[i] | ||
} | ||
} | ||
return defaultProp | ||
} | ||
Oops, something went wrong.
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.
批准更改并建议记录使用案例
mergeProp
函数的实现正确地处理了属性列表,并返回第一个非 undefined 值,后面的属性具有更高的优先级。这对于处理已弃用的属性非常有用。建议在文档中添加此函数的使用案例,以帮助开发者了解其在实际场景中的应用。