I'm currently experimenting with typescript generics and I'm trying to automatically infer the type of fn
, also known as P
, but it doesn't seem to be working as intended.
If you want to check out the code yourself, visit this interactive Playground link
Below is a snippet of the code I'm working on:
type Callback = (...args: any[]) => any
interface Route<
T extends Callback
> {
fn: T
}
function route <
P extends Callback,
R extends Route<P>
> (pathname: string, handler: R) {
return handler.fn
}
const x = route('/hi', {fn: (name: string) => `hi ${name}`})
// ^?
My expectation was for x
to have the type (name: string) => string
, however, it instead returned Callback
.