π 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.