如何访问Typescript中的组合函数并添加其类型?

时间:2019-04-20 14:45:43

标签: typescript typescript-typings

我试图为类React Component中的组合函数定义类型,但没有成功:

接口:

interface CalendarProps {
  date: Date;
  onChange: (formatDate: any) => void;
  current: Date | null;
  pressureTimeout: any
}

interface HandlePressureProps {
  handlePressure: () => any
  pressureTimeout: () => any
}

类组件:

class Calendar extends Component<CalendarProps> {
  public state = { ...this.resolveStateFromProp(), today: new Date() };

 private handlePressure = (fn: () => void): HandlePressureProps => {
    if (typeof fn === "function") {
      fn();
      this.pressureTimeout = setTimeout(() => {
        this.pressureTimer = setInterval(fn, 100);
      }, 500);
    }
  };

  private clearPressureTimer = () => {
    this.pressureTimer && clearInterval(this.pressureTimer);
    this.pressureTimeout && clearTimeout(this.pressureTimeout);
  };
  

HandlePressureProps出现以下错误:

A function whose declared type is neither 'void' nor 'any' must return a value

pressureTimeoutpressureTimer返回错误:

Property 'pressureTimeout' does not exist on type 'Calendar'

handlePressure函数中,我尝试在HandlePressureProps中定义类型。 在clearPressureTimer中,我让它在CalendarProps界面中定义。

由于我都无法“访问”它,所以我都没有成功。

任何提示将不胜感激。谢谢

0 个答案:

没有答案