TypeScript接受Function的类型作为类型,但不接受显式输入的函数作为类型

时间:2020-10-19 09:21:09

标签: reactjs typescript

我有一个React组件:

const Component = (props: Props): ReactElement => {

    const transfer = onTransfer()(props.onTransfer, props.selected);

    return (
      <div
        onClick={transfer}

onTransfer函数的位置为:

export const onTransfer = (): (cb: OnTransfer, selected: boolean) => void => (cb: OnTransfer, selected: boolean): void => {
    cb({
        type: selected
            ? 'select'
            : 'deselect',
    });
};

此实现引发TypeScript错误:

不能将类型'void'分配给类型'((event:MouseEvent )=> void)| undefined'.ts(2322)

但是,如果我将onTransfer的实现方式更改为我使用Function类型的实现,则:

export const onTransfer = (): Function => (cb: OnTransfer, selected: boolean): void => {
    cb({
        type: selected
            ? 'select'
            : 'deselect',
    });
};

TypeScript不抱怨。

在最新版本的短绒棉中,使用Function类型会产生棉绒错误。

如何显式键入onTransfer函数返回的函数而没有错误?

0 个答案:

没有答案
相关问题