Imagine if I were to create a type called CoolType
in this manner:
type CoolType<T> = {message: string, result: T}
Next, let's define a type called CoolFunction
which represents a function that returns CoolType
:
type CoolFunction = <T>() => CoolType<T>
CoolFunction
is the type expected as a parameter by another function:
function superCoolFunction(coolFunction: CoolFunction) {
return coolFunction()
}
After all these definitions, We try running some code like this:
const res = superCoolFunction(<string>() => {
return {message: 'I am the message', result: 'I am the result'}
})
However, when reaching <string>() => {
, an error is thrown by the compiler stating that
'string' is declared but its value is never read.ts(6133) Argument of type '() => { message: string; result: string; }' is not assignable to parameter of type 'CoolFunction'. Call signature return types '{ message: string; result: string; }' and 'CoolType' are incompatible. The types of 'result' are incompatible between these types. Type 'string' is not assignable to type 'T'. 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.ts(2345)
Do you have any thoughts on what might be causing this issue? Here's a stackblitz link for reference.