我试图为类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
pressureTimeout
和pressureTimer
返回错误:
Property 'pressureTimeout' does not exist on type 'Calendar'
在handlePressure
函数中,我尝试在HandlePressureProps
中定义类型。
在clearPressureTimer中,我让它在CalendarProps界面中定义。
由于我都无法“访问”它,所以我都没有成功。
任何提示将不胜感激。谢谢