0

Why does Typescript raises an error when I try to call a generic function, from an arrow function using the supplied type parameter.

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

EDIT

This question arose while I was trying to write a generic arrow function similar to c in a .tsx file.

In this case, all of the following syntaxes raises an error.

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)

In the end, I chose to use the regular function syntax.

1 Answer 1

2

Because you declare T in the type definition of c. That is how it should look

const c: <T>() => void = <T>() => a<T>()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.