tjjfvi#0
`!tjjfvi:thats-no-generic-its-a-type-cast`:
When you use a type parameter in the function, it should generally be inferrable from the arguments.
For example, this is a misuse of generics:
function getMeA<T>(): T {
}
because getMeA<string>() and getMeA<number>() compile to the same code at runtime, there's so way to implement this function safely (other than always throwing); this is just a type cast in disguise. Instead of using a generic here, you should return unknown, and cast at the call site if necessary, to be clear it's an unsafe operation:
-function getMeA<T>(): T {
+function getMeA(): unknown {
/* magic */
}
-getMeA<number>()
+getMeA() as number
One exception to this rule is if you're returning a possibly-empty container of T. For example, these are all perfectly safe, even though the generic can't be inferred from the parameters:
function emptyArray<T>(): Array<T> {
return []
}
function useRef<T>(): { current?: T } {
return {}
}