打字稿从状态指定返回类型为一种或多种

时间:2019-03-29 16:43:24

标签: reactjs typescript

如何从状态:IMyContainerState中指定返回类型为一个或多个? 在下面的代码中,我将返回类型键入了对象{} |空值... 我不想指定{},而是指定一个接口或某种 PICK声明。

我要弄清楚的是Pick可能是 只是状态属性之一或许多状态属性。

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; }
    }

1 个答案:

答案 0 :(得分:1)

您也许可以使用Partial<IMyContainerState>类型? doc

相关问题