我有一个自定义钩子,很不幸,该钩子是用TypeScript .tsx文件中使用的纯JS编写的。
我这样称呼
const [toggleDictation,dictationResults] = useDictation()
useEffect(() => {
//start recognizing
toggleDictation();
},[]);
我在编译器中看到一条错误消息:
This expression is not callable.
Not all constituents of type 'string | boolean | never[] | (() => Promise<void>)' are callable.
Type 'string' has no call signatures.ts(2349)
自定义JS挂钩中的toggleDictation函数写为:
const toggleDictation = async () => {
try {
if (!speechHasStarted) {
await _startRecognizing();
return;
} else {
await _stopRecognizing();
return;
}
} catch (e) {
//eslint-disable-next-line
console.error("Error in toggleDictation,",e);
return;
}
};
是否有一种方法可以强制转换或断言以正确设置函数类型以消除错误?