在泛型箭头函数中调用泛型函数时出现错误 TS2304

Error TS2304 when calling generic function inside generic arrow function

当我尝试使用提供的类型参数从箭头函数调用泛型函数时,为什么 Typescript 会引发错误。

function a<T>() { }
function b<T>() { a<T>() } // no errors
const c: <T>() => void = () => a<T>() // cannot find name T. ts(2304)

编辑

这个问题是在我尝试编写通用箭头函数时出现的 类似于 .tsx 文件中的 c

在这种情况下,以下所有语法都会引发错误。

const c: <T>() => void = <T>() => a<T>()
    //^ Type 'Element' is not assignable to type '<T>() => void'.
       // Type 'Element' provides no match for the signature '<T>(): void'.ts(2322)
const c = <T>() => a<T>() 
         //^ JSX element 'T' has no corresponding closing tag.ts(17008)

最终,我选择了使用正则函数语法。

因为你在c的类型定义中声明了T。 它应该是这样的

const c: <T>() => void = <T>() => a<T>()