I'm struggling to develop a function that accepts another function which, when called, should return a specific type of value. If the value itself is passed, it should just return that value.
Unfortunately, I am facing difficulty in determining the final output type.
type Test<T> = (() => T) | T
class List{
constructor(protected fnOrNumber: Test<number>) {
//fnOrNumber will sometimes be number or function that returns number
}
get list(): number{
return resolve(this.fnOrNumber)
// ^^^
//Type 'Test<number>' is not assignable to type 'number'.
//Type '() => number' is not assignable to type 'number'.
}
}
//function to resolve the final value
function resolve<T>(v: T): T{
if (typeof v === 'function') {
return v()
}
return v
}