reactjs 如何编写一个Typescript类型安全事件以传递给React-select中的onclick函数

pgpifvop  于 2023-01-25  发布在  React
关注(0)|答案(1)|浏览(125)

我想让它类型安全,但是编译器编译成功了。我错过了什么?
实际操作中.ts

export type AnActionType = ( index: number, value?: string ) => ReduxAsyncAction

在组件.tsx中

const changeHandler = (value, action : AnActionType ) => {
  action(value); //expects to complain on missing arg
  action(value, 1); // expects to complain on arg types
}

<Select onchange={ value => changeHandler(value, action ) } />

我应该如何编写代码?

w1jd8yoj

w1jd8yoj1#

尝试在签名中添加:number以替换value,如下所示:

const changeHandler = (value: number, action: AnActionType ): void => {
    action(value); //expects to complain on missing arg
    action(value, 1); // expects to complain on arg types
}

相关问题