π Search Terms
Return type, inference, cotravariant
π Version & Regression Information
It seems to be in any version on the playground.
β― Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEFEA8EMFsAcA2BTUBGAUAFwJ71QMID2WATtAG7SkCW0AdlgDwAaAfKALygAUAxgC5QLAJRcOFIjQAmAbgwZpyPomqoAZgFd6fLDSL1QpIpqzImAJWQBHTTVLIAzmx5YhAb1B8DZyG9DEZJTUdIyWNnYOjqAAZKCeAEZCjmQ09ADmoAC+HFkA2gC6IkIMOPKKyqoOoFo6egY19EwAKqDIfsj00tHuWQA0oBYu6kL8Qs1inBwWxfFePu3+geRUtAzMzWwD6vSjgqAT4oPZ5cam5p7QyakZ2S4YoKB5O-xH9MgA7oNO8AaOyPwAHTQUAAai8gISIhEBQwIgUZzMPAeTxefDen2+jl+9H+QKhMLhGCAA
π» Code
// Example 1
type Cotravariant<X> = (c: X) => void;
declare function route<Requires>(t: { context: Cotravariant<Requires & { b: string }> }[]): any;
declare function fn<T extends {}, R>(f: (c: T) => R): { context: Cotravariant<T>, fn: (c: T) => R };
route<{ a: string }>(
[fn(c => new Response(c.a + c.b))] // This infers b
)
route(
[fn(c => new Response(c.b))] // This doesn't infer b
)
π Actual behavior
The second callback doesn't infer c.b.
π Expected behavior
The second callback should infer c.b.
Additional information about the issue
I have no idea if this is working as intended or not, however it would be really nice if it works as it allows inference for non-chaining API, something like:
init(
'/:id', // Inferred c.params.id from here
[send], // Inferred c.send from here
[route('GET', '/', (c) => c.send.body(c.params.id))]
)
My implementation looks like this: https://github.com/mapljs/framework/blob/main/src/index.ts#L65
π Search Terms
Return type, inference, cotravariant
π Version & Regression Information
It seems to be in any version on the playground.
β― Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEFEA8EMFsAcA2BTUBGAUAFwJ71QMID2WATtAG7SkCW0AdlgDwAaAfKALygAUAxgC5QLAJRcOFIjQAmAbgwZpyPomqoAZgFd6fLDSL1QpIpqzImAJWQBHTTVLIAzmx5YhAb1B8DZyG9DEZJTUdIyWNnYOjqAAZKCeAEZCjmQ09ADmoAC+HFkA2gC6IkIMOPKKyqoOoFo6egY19EwAKqDIfsj00tHuWQA0oBYu6kL8Qs1inBwWxfFePu3+geRUtAzMzWwD6vSjgqAT4oPZ5cam5p7QyakZ2S4YoKB5O-xH9MgA7oNO8AaOyPwAHTQUAAai8gISIhEBQwIgUZzMPAeTxefDen2+jl+9H+QKhMLhGCAA
π» Code
π Actual behavior
The second callback doesn't infer
c.b.π Expected behavior
The second callback should infer
c.b.Additional information about the issue
I have no idea if this is working as intended or not, however it would be really nice if it works as it allows inference for non-chaining API, something like:
My implementation looks like this: https://github.com/mapljs/framework/blob/main/src/index.ts#L65