Skip to content

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

Closed
@Andarist

Description

@Andarist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions