Skip to content

Return type inference limitationΒ #63697

Description

@aquapi

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions