胖箭头功能/回调的类型是什么?

问题描述 投票:-1回答:1

相当简单的问题。我来自Qt / C ++背景,因此我认为这是明确的良好做法,因此

protected sanityCheck() : void { ... } // Braced and Typepilled
protected sanityCheck() { ... }        // Extremely stupid and dangerous 

嗯,我该如何明确:

protected checkRadio( index : number ) : ????? // "Function" type does not work
{
        return (event: React.MouseEvent) => 
        {
                let radioIndex : HTMLInputElement 
                        = document.getElementsByName(this.question())[index] 
                        as HTMLInputElement;
                radioIndex.checked = true;
                event.preventDefault();
        }
}

我尝试过的大多数类型都导致了

Type 'void' is not assignable to type 
'((event: MouseEvent<HTMLLIElement, MouseEvent>) => void) | undefined'.  TS2322

当我检查文档时,它什么也没说。当我检查typeof时,它只是说function。我得到的印象是可以定义自己的类型,但是我想一次过,一种类型的解决方案要尽可能具体。

到目前为止,我能做的最好的事情是any,这是最不明确的类型。

那么,我可以为此功能使用的最具体和最明确的类型是什么?

谢谢。

相当简单的问题。我来自Qt / C ++背景,因此我认为这是明确的好习惯,因此受保护的sanityCheck():void {...} //受括号和Typepilled保护的...

javascript reactjs typescript types callback
1个回答
1
投票

使用React.EventHandler<React.SyntheticEvent>React.EventHandler<any>作为返回类型

© www.soinside.com 2019 - 2024. All rights reserved.