-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Boolean narrowed to true only when generic type is explicitly provided while it shouldn't #59738
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
Comments
Sort of a duplicate of #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, 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 |
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 |
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...The text was updated successfully, but these errors were encountered: