Open Harpush opened 1 month ago
Sort of a duplicate of https://github.com/microsoft/TypeScript/issues/48363 but with object types. It repros the same way when I wrap function arguments from that issue in objects: TS playground.
Just adding that it also happened for unions
Well, boolean
is a union of true | false
so it makes sense that unions are affected π It's just that boolean
is expected to behave more like a primitive than a union - hence the issue I linked.
The union case can be reproduced easily using the same kind of code (TS playground):
type Box<T> = {
get: () => T;
set: (value: T) => void;
};
declare function box<T>(value: T): Box<T>;
const bb1: Box<"foo" | "bar"> = box("foo"); // errors today
const bb2: Box<{ prop: "foo" | "bar" }> = box({ prop: "foo" }); // errors today
The algorithm can only keep the literal type based on the contextual type. There is no mechanism to widen it to the contextual union type when that position refers to an invariant type parameter. I think this issue here should stay focused on the invariant problem and I'll create a new one for this specific issue with boolean
: https://github.com/microsoft/TypeScript/issues/59754
What's a repro that doesn't depend on an incorrect variance annotation?
@RyanCavanaugh I think it's the one I posted above, the one with "foo" | "bar"
.
What's a repro that doesn't depend on an incorrect variance annotation?
Replace
//Use the variance annotation to force this type to be invariant, even though it would otherwise be covariant
type Invariant<in out T> = () => T;
with
type Invariant = (_:T) => T;
and the error is the same.
I don't see an error after removing the variance annotation
@RyanCavanaugh anything else needed here? It is still marked with needs more info
π Search Terms
specify type narrow union
π Version & Regression Information
All versions I checked and nothing in FAQ that seems related
β― Playground Link
https://www.typescriptlang.org/play/?#code/LAKA9GCqDOCmAEAXAFggbgQwE4EsMDsBjBA-Ae0Q0RzPyTPgDMytilkdokBPABwUQMARghz5MuAogA08WGlh0UZAK4BzZPByJ4Ad1UAbACbwKqLLs4IR8QmQl58iUIj4IAkuOyPEAHjGmKjoAKgB88AC88AAUAJSR4cEA3KCgdvjQOswMUb7BcgAeiIpGXGRCAFawhIih0eUVAFzwwbHNng5SvgDeANoAClp0ANaw3GSMLQC67V6STnkDU6EAvuER4fgqBgYAhCkg6ZnwQtiR8HmFxfil8H2DAaPjk8Ez8B3eXQTcq3W6za0AQl4FsdvtUodaMcxIxYFgsLATFFTlhot0MM1smihM1EFgVLAVrEiQcIPByRTKVSAHoAflAZKOOhRzR6DLAVIx7zmPjZ4A5VIpOO5nQWQjIZAMsAIoVJAopazl8DW8AAdOr2bYoVkJazuprKcLxZLpfglWtNXiCVouGJilheAjiiYMFwAETGqUEN0QiAAUQKvAMOEI2gM3Hg0H4ocYEZQCDUijhIfgvGwGAAtmltYUgyHtOcUXzKVyPvM-PqQILycKy7zPabZaBKRaQGs0Vysd1hVbCcTYkl4P6AErDgDyw4NFLpmqZTF1F0rZMNuPxsHNoXZ5F07CoSDXINgiK4ghOCA9Eq9+DdsgLnCG9sdsGd8Fd8DdvZ9IHZAbzocQ4aRtGOCxuwCZJrghCpumGbzlgJxnAQJjZNmGQ6LAgbBv+ABMhbYMWFKljyXSVtWJyzKKfgNjKBwtpubZ1OimISgRNbNNRZqgO23argSRIkkOYD6FgwzQL6YC-lhYYRlG1QgXGqDwIm+DJlBaZYJmcEIfBSG2K6Oi9q+XAcahxwYX+2gAMx4VgrGvhRnwLKR1a1sRYqXo2tEKvR3GdhK2K8SQxkeQQ-EDoJwmieyABCIX4NEvbxBgBjQAwkViSAQA
π» Code
π Actual behavior
Narrows to
true
π Expected behavior
Should stay
boolean
Additional information about the issue
Doing
as boolean
works...