3

interface IRoleAddProps {
    roles: Array<IRole>
}
interface IRoleAddState {
    current: IRole | null
}
class RoleAdd extends React.Component<IRoleAddProps, IRoleAddState> {
    state = {
        current: null,
    }
    renderNoneSelect = () => {
        return (
            <div styleName="empty">
                <SvgIcon name="arrow" styleName="icon-arrow" />
                <span>Empty</span>
            </div>
        )
    }
    onRoleClick = (role: IRole) => {
        this.setState({
            current: role,
        })
    }
    render() {
        const { roles } = this.props
        const current = this.state.current

        return (
            <div styleName="role-add">
                <div styleName="role-list">
                    <div styleName="title">Select role:</div>
                    <div styleName="list">
                        {roles.map(role => {
                            const cls = classNames({
                                item: true,
                                active: current && ( current.id === role.id )
                            })

                            return (
                                <div
                                    key={role.id}
                                    styleName={cls}
                                    className="g-text-inline"
                                    onClick={this.onRoleClick.bind(this, role)}
                                >
                                    <CheckBox />
                                    <span>{role.name}</span>
                                </div>
                            )
                        })}
                    </div>
                </div>
                <div styleName="view">
                    {!current && this.renderNoneSelect()}
                    {current && 'view'}
                </div>
            </div>
        )
    }
}

export default RoleAdd

The code like this, but TS still tells me:

enter image description here

Even I tried:

enter image description here

And "!" also doesn't work

enter image description here

As you can see the "current" object can't be null because i have null check before i use it.

But typescript engine still show me that error.

I'm wondering is that because i initialized current object with null value, but ts can not figure out types from setState, so it takes current always null?

1
  • 1
    It doesn't work because state member is overridden in derived class and it gets inferred type of provided initial value Commented Jan 24, 2019 at 13:53

2 Answers 2

4

You'll need to assign a type to state, like

state: IRoleAddState = {
    current: null
};

Then, state will be of type IRoleAddState and not { current: null }. After that, the methods you tried will work.

Sign up to request clarification or add additional context in comments.

Comments

3

Explicitly defining the state in a constructor should solve the issue.

constructor(props) {
    super(props);

    this.state = {
        current: null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.