[英]Attempting to typecheck a function in Typescript
I am not sure how to describe my problem, but essentially I am attempting to type-check a function but I am unsure if my understanding is correct.我不确定如何描述我的问题,但基本上我正在尝试对 function 进行类型检查,但我不确定我的理解是否正确。
I have these props over here我这里有这些道具
const { closeModal, productId, activeEnvironment } = props;
Now I already know that productId is a string and so is activeEnvironment, but closeModal is a function, now I figured this out by outputting it to the console in a console log statement.现在我已经知道 productId 是一个字符串,activeEnvironment 也是,但是 closeModal 是一个 function,现在我通过在控制台日志语句中将其输出到控制台来解决这个问题。
This is what was outputted in the browser console.这是在浏览器控制台中输出的内容。
ƒ closeModal() {
setManualBeatModalOpen(false);
}
Here is the usage of my ManualPulseModal这是我的 ManualPulseModal 的用法
<ManualBeatForm
productId={productId}
closeModal={() => {
setManualBeatModalOpen(false);
}}
activeEnvironment={activeEnvironment}
/>
Now how would I type check the closeModal variable do I need to use Dispatch....etc现在我将如何键入检查 closeModal 变量是否需要使用 Dispatch....等
Since the closeModal
has no parameters and no return value, you can type it simply as closeModal: () => void
.由于
closeModal
没有参数也没有返回值,因此您可以简单地将其键入为closeModal: () => void
。
So you could have something similar to this (excluding the other props):所以你可以有类似的东西(不包括其他道具):
interface Props {
closeModal : () => void
}
export function YourFunc(props : Props) {
const { closeModal } = props;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.