TypeScript环境下未返回ownProps导致mapStateToProps出现类型错误的问题咨询
Let's break down what's going on here, why you're seeing that TypeScript error, and how to resolve it while addressing your question about Redux's documentation.
The Root Cause of the Error
Your TypeScript error comes from a type inference mismatch between your component's requirements and how React-Redux's connect function calculates the final props passed to your component:
- Your
MyComponentexpectsname: stringas a required prop (defined inMyComponentProps). - Your
mapStateToPropsreturns two possible types:{ name: string }ifownPropslacks aname(though this check is actually unreachable—sinceMyComponentPropsmarksnameas required,ownPropswill always have it!){}(empty object) ifownPropsdoes includename.
TypeScript can't reliably confirm that when mapStateToProps returns {}, the ownProps will definitely supply the required name prop. It ends up inferring that name could be undefined, which conflicts with your component's required string type.
A quick key note: That !("name" in ownProps) check is dead code right now. Since MyComponentProps requires name, TypeScript and runtime will never hit that branch.
Why Returning {...ownProps} Fixes the Issue
When you return {...ownProps} instead of {}, you're guaranteeing that mapStateToProps always outputs an object containing all required props from ownProps (including name). This lets TypeScript confirm the final props passed to MyComponent will always have a valid name: string, matching the component's prop type exactly.
Does This Violate Redux's Documentation?
Redux's docs state you don't need to return ownProps unless you're modifying them—and in this case, you're just passing them through unaltered. This is a valid workaround for a TypeScript type inference limitation in React-Redux's connect system, not a violation of best practices.
At runtime, this approach doesn't cause any issues: React-Redux will still merge ownProps, mapStateToProps results, and mapDispatchToProps results correctly. Returning {...ownProps} just helps TypeScript "see" that all required props are present.
A Cleaner Alternative
Since your name prop is required in MyComponentProps, you can simplify mapStateToProps to remove the unnecessary conditional entirely:
const mapStateToProps = (state: RootState, ownProps: MyComponentProps) => { // Explicitly return the required name prop to satisfy TypeScript return { name: ownProps.name }; };
If you actually want to provide a default name when it might be missing, adjust MyComponentProps to make name optional first:
interface MyComponentProps { name?: string; // Mark as optional age: number; } const mapStateToProps = (state: RootState, ownProps: MyComponentProps) => { // Provide a default if name is missing from ownProps return { name: ownProps.name ?? "foo" }; };
This makes your intent clear to TypeScript, ensuring it understands name could be missing from ownProps and that your mapStateToProps will always supply a valid string.
内容的提问来源于stack exchange,提问作者Philip Scadding

