简体   繁体   English

试图在 Typescript 中对 function 进行类型检查

[英]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.

相关问题 如何对流中的多态函数进行类型检查? - How to typecheck polymorphic function in flow? 如何使用 Typescript 对具有后备值的解构 object 进行类型检查? - How to typecheck a destructured object with fallback values with Typescript? 尝试在TypeScript中使用串行端口时,未捕获到的TypeError:不是函数 - Uncaught TypeError: exists is not a function when attempting to use serial port in TypeScript 如何在Flow中使用相同泛型类型的值来检查泛型函数 - How to typecheck a generic function using a value of that same generic type in Flow 涉及通用对象的泛型属性的赋值无法在泛型函数内正确地进行类型检查 - Assignment involving generic property of generic object fails to typecheck correctly within generic function 尝试将状态传递给 React 中的子组件 Typescript - Attempting to pass states to children component in React Typescript 是否可以使用prop-types来检查常规的Object,Function参数和返回值? - Is it possible to use prop-types to typeCheck regular Object, Function parameters and return value? 试图将字符串传递给构造的 function - Attempting to pass a string to a constructed function 这是 typescript function 吗? - Is this a typescript function? 尝试使用推功能获取复选框的值 - Attempting to use the push function to get values of checkboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM