You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React+Redux+TypeScript项目报错:无法读取未定义对象的'shape'属性

Fixing "Cannot read property 'shape' of undefined" in Your React/Redux/TypeScript Project

Hey there! Let's break down why you're hitting this error and how to fix it—it's a common snag, especially when mixing TypeScript with prop-types.

Common Causes & Solutions

1. Incorrect Prop-Types Import

The most likely culprit is that PropTypes isn't being imported properly in the file where you're using shape(). Double-check your import statement:

Make sure it’s written like this:

import PropTypes from 'prop-types';

Avoid wildcard imports like import * as PropTypes from 'prop-types' with older versions of the library—this can sometimes lead to PropTypes being undefined in certain contexts.

2. Unnecessary Use of Prop-Types in TypeScript

Since you’re using TypeScript, you don’t actually need prop-types for type checking. TS’s built-in type system is more powerful and deeply integrated with React. Using both can lead to conflicts and unnecessary errors.

Replace prop-type definitions with TypeScript interfaces or type aliases instead. For example:

// Instead of PropTypes.shape({ ... })
interface UserProps {
  id: number;
  name: string;
  email: string;
}

interface MyComponentProps {
  user: UserProps;
}

const MyComponent: React.FC<MyComponentProps> = ({ user }) => {
  return <div>{user.name}</div>;
};

This removes the need for PropTypes entirely and eliminates the error at its source.

3. Corrupted Dependencies

Sometimes incomplete or corrupted package installations cause weird issues like this. Try resetting your dependencies:

# Delete node_modules and lock file
rm -rf node_modules package-lock.json

# Reinstall dependencies
npm install

If you use Yarn, swap out the npm commands for yarn install after deleting yarn.lock.

4. Typos or Misused Methods

Double-check for typos in your code—like writing PropType.shape() (missing the final "s") instead of PropTypes.shape(). Also, make sure you’re not accidentally trying to use shape() from another library (like Redux) that doesn’t expose this method.

Final Notes

If you still want to use prop-types alongside TypeScript (for runtime checks in production, for example), ensure your import is correct and avoid mixing type systems unnecessarily. But for most TS projects, sticking with native TypeScript types is the cleaner, more reliable approach.

内容的提问来源于stack exchange,提问作者Rohit Choudhary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:26:29