Acts7Seven
Acts7Seven

Reputation: 417

Typescript Specify return type as one or multiple from state

How can I specify that the return type will be one or many from my state: IMyContainerState? In the code below I've typed the return type to object {} | null... Rather than {} I'd like to specify an interface or some sort of PICK statement.

What I'm trying to figure out is the syntax when Pick could be just one of the state properties or many of the state properties.

interface IMyContainerState {
    redirect: boolean;
    request: boolean;
    notify: void;
    totalCount: number;
}


public static getDerivedStateFromProps: IArrowFunction
    = (nextProps: IMyContainerProps, prevState: IMyContainerState): {} | null => {
        if (!!nextProps.data.request && nextProps.data.request !== prevState.request) {

            // what if this code returned request AND redirect properties from state?
            return { request: nextProps.data.request };

        } else { return null; }
    }

Upvotes: 0

Views: 96

Answers (1)

antoinechalifour
antoinechalifour

Reputation: 495

You could maybe use the Partial<IMyContainerState> type ? doc

Upvotes: 1

Related Questions