Skip to content

Contravariant inference gets picked when a a better covariant candidate exists #57908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Andarist opened this issue Mar 22, 2024 · 0 comments Β· Fixed by #57909
Closed

Contravariant inference gets picked when a a better covariant candidate exists #57908

Andarist opened this issue Mar 22, 2024 · 0 comments Β· Fixed by #57909
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

πŸ”Ž Search Terms

covariant covariance contravariant contravariance inference candidates subtyping assignable subtype

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240322#code/C4TwDgpgBAShCOBXCBnYAeAKgZQMYAsIBbAQyggA9gIA7AExSj0NID4oBeKAbwCgooSCACcQALig4CxEgG0ARENHyAugG5eAXw29QkJtNKceg5KID8ExDQDWNAPYB3GmqgAje3RCWo1u05oobV5eOghcABsSYWgAM2tcYABLe0Dhe0RqLGYZcipaBgMWElYACns3ACsJPgEwGIlSkgkpYoBKTnYAN3skug0BFENmyRzSAah8EnoIiEaY+Ak4ITRs4dYOjm7e-q02iR6+nVxUtCgukgi+kmpjUoB9GtMRbytbB2dXDy8fPw-AzSbdjcYK8dKZCClWpQepzc6Xa7UAA0-CgQ2KNVRAiU4ig8nkKIEmkJk2mdFm8wQQJ4WKgJxoZ2oaAkaGESRoAHNjAsAHQ4ibEvY6ShgezCYA8YJAA

πŸ’» Code

type Request<TSchema extends Schema> = {
  query: TSchema["query"];
};

type Schema = { query?: unknown; body?: unknown };

declare function route<TSchema extends Schema>(obj: {
  pre: (a: TSchema) => void;
  schema: TSchema;
  handle: (req: Request<TSchema>) => void;
}): void;

const validate = (_: { query?: unknown; body?: unknown }) => {};

route({
  pre: validate,
  schema: {
    query: "",
  },
  handle: (req) => {
    const test: string = req.query;
  },
});

export {};

πŸ™ Actual behavior

assignment to test fails because { query?: unknown; body?: unknown; } was inferred there

πŸ™‚ Expected behavior

I'd expect { query: string } to be inferred here and the assignment to test to succeed

Additional information about the issue

This is a reduced version of this failure.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Mar 22, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants