Imagine we have two types, that are similar, though not the same:
type First<G> = (arg: G) => G
type Second = <G>(arg: G) => G
Now obviously when talking about these two types, there are differences, I am wondering, if someone can explain, in detail, what those differences are, how do we refer to them, they are both "generics" but do they have a better, more distinct name, so that I can search for this, because I couldn't find anything.
From what I gather - First
type requires us to specify a generic when defining a variable of said type whereas the Second
type infers it from usage, depending on the line of code where we used it. Are there any other benefits? Or negatives? One particular issue I am having with the Second
form is the fact that I cannot assert to G
I've also ran into an issue of following:
const myFunction: Second = (arg) => {
let someValue = getExternalValue() // typeof someValue === unknown
return someValue as G // this is an issue, I cannot access G
}
Now I know assertions are bad, and all that, but is there a way of writing this to access this generic G
? Obviously the example is not a real codebase code, just demonstrating a point.