代码之家  ›  专栏  ›  技术社区  ›  Joon

连接出现Typescript Redux编译错误(组件与签名不匹配)

  •  0
  • Joon  · 技术社区  · 8 年前

    我有一个无法编译的typescript react/redux组件。我有其他几乎完全相同的组件,可以编译。我需要理解为什么这个没有编译。

    import * as React from 'react';
    import { connect } from 'react-redux';
    import { ApplicationState } from '../store';
    
    interface ToastProps {
        messages: string[]
    }
    
     interface FadeState {
        visible: boolean
    }
    
    class ToastBar extends React.Component<ToastProps, FadeState> {
        public render() {
            if (this.props.messages.length > 0) {
                this.state = { visible: true };
            } else {
                this.state = { visible: false };
            }
            let key = 0;
            return <div id="toastbar" className={this.state.visible ? 'show' : ''}>
                {this.props.messages.map(function (listValue) {
                    return <p key={++key}>{listValue}</p>;
                })}            
            </div>;
        }
    }
    
    const mapStateToProps = (state: ApplicationState, ownProps?: ToastProps) => ({
        messages: state ? state.toastMessages.messages : []
    });
    
    export default connect(mapStateToProps, {}, null, { pure: false })(ToastBar);
    

    我从IDE和运行npm运行脚本生成时收到以下错误消息:

    (9,82)“'typeof ToastBar'类型的参数不能赋值给” “ComponentType>”类型的参数。类型“typeof ToastBar”不可分配给类型 '无状态组件>'。 ReactNode;},上下文?:any):元素| null’。

    下面是另一个编译良好的组件:

    import * as UserStore from '../store/UserManagement';
    import * as React from 'react';
    import { RouteComponentProps } from 'react-router';
    import { ApplicationState } from '../store';
    import { connect } from 'react-redux';
    import { User, UserRole } from '../model/User';
    import { UserEditDialog } from './UserEditDialog';
    
    interface UserMgmtState {
        isLoading: boolean;
        users: User[];
        currentUser: User;
    }
    
    interface UserEditState {
        editingUser: User | null;
    }
    
    type DispatchProps = typeof UserStore.actionCreators;
    type UserManagementProps = UserMgmtState & DispatchProps & RouteComponentProps<{}>;
    
    class UserManagement extends React.Component<UserManagementProps, UserEditState> {
    
        componentWillMount() {
            // snipped
        }
    
        editUser(user: User) {
            this.setState({
                editingUser: user
            }); 
        }
    
        stopEditingUser() {
            this.setState({
                editingUser: null
            });
        }
    
        deleteUser(user: User) {
            if (confirm('Delete ' + user.fullName + '. Are you sure?'))
                this.props.deleteUser(user.name);
        }
    
        userIsCurrentUser(user: User): boolean {
            return this.props.currentUser.name == user.name;
        }
    
        saveUserEdit(user: User) {
            this.props.updateUser(user);
            this.setState({
                editingUser: null
            });
        }
    
        displayRoles(roles: UserRole[]): string {
            return roles.join(', ');
        }
    
        renderUsersTable(): any {
            if (this.props.isLoading) {
                return <div>Loading users</div>;
            }
            else {
                return <div>
                    <!-- snipped -->
                 </div>;
            }
        }
    
        public render() {
            return <div>
                <h1>User Management</h1>
                {this.renderUsersTable()}
            </div>;
        }
    }
    
    const mapStateToProps = (state: ApplicationState, ownProps?: UserMgmtState) => ({
        isLoading: state ? state.userManagement.isLoading : false,
        users: state ? state.userManagement.users : [],
        currentUser: state ? state.loggedInUser.currentUser : null
    });
    
    export default connect(
        mapStateToProps, // Selects which state properties are merged into the component's props
        UserStore.actionCreators // Selects which action creators are merged into the component's props
    )(UserManagement);
    

    为什么用户管理编译,而Toastbar不编译?我真的很迷惑。。。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Joon    8 年前

    问题是状态.toastMessages.messages属性的类型为字符串[],而toastrops.messages文件属性的类型为字符串[]

    如果其他人也有类似的问题,并且想知道如何进行故障排除:为了解决这个问题,我将一行mapstatetops方法更改为多行方法来定义并返回结果,然后编译问题就出现了。一旦我知道mapstatetops的输出有一个不兼容的类型,我就检查了状态.toastMessages.messages发现了问题

    推荐文章