[英]Get function argument type on function declaration
I'm building a custom React hook which accepts an object (which has a function as one of the properties) as argument.我正在构建一个自定义 React 挂钩,它接受一个 object(其中一个属性是 function)作为参数。
I want to get type definition on that function's arguments, which I am currently able to do like this:我想获得该函数的 arguments 的类型定义,我目前可以这样做:
const { submitHandler, register } = useForm({
handleSubmit: data => {
/*
Everything is ok:
(parameter) data: {
inputs: InputRefs;
e: React.FormEvent<HTMLFormElement>;
}
*/
}
});
However, I'd like to split the code by getting handleSubmit
outside the useForm
argument object like so:但是,我想通过在
useForm
参数 object 之外获取handleSubmit
来拆分代码,如下所示:
const handleSubmit = data => {
// Parameter 'data' implicitly has an 'any' type, but a better type may be inferred from usage.
};
const { submitHandler, register } = useForm({ handleSubmit });
But if I do this, data
is of type "any"... Is there a way to get the type definition for that function's arguments?但是如果我这样做,
data
的类型是“任何”...有没有办法获得该函数的 arguments 的类型定义? Or the only and correct solution is really just export/import the type?或者唯一正确的解决方案真的只是导出/导入类型?
Sorry if this is not explicit or has already been explained, but I just started learning typescript and I can't seem to find what are the correct keywords to find the answer to what I am asking...抱歉,如果这不是明确的或已经解释过,但我刚刚开始学习 typescript 并且我似乎无法找到正确的关键字来找到我所问问题的答案......
Thank you谢谢
Right-click on handleSubmit
here:在此处右键单击
handleSubmit
:
const { submitHandler, register } = useForm({
handleSubmit: data => {
/*
Everything is ok:
(parameter) data: {
inputs: InputRefs;
e: React.FormEvent<HTMLFormElement>;
}
*/
}
});
and choose "Go to Definition".并选择“转到定义”。 That will give you the type you need to specify here:
这将为您提供您需要在此处指定的类型:
const handleSubmit: ${Type} = data => {
// Parameter 'data' implicitly has an 'any' type, but a better type may be inferred from usage.
};
const { submitHandler, register } = useForm({ handleSubmit });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.