-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Overly-narrowed generic object loses typing #57804
Comments
Your bottom examples "work" because declare let f: CommonUtilFns<A> | CommonUtilFns<B>
declare let g: CommonUtilFns<A | B>
f = g; // error!
g = f; // error! Your top example doesn't work because it's essentially #30581. TS does not keep track of the identity of interface UtilMap {
A: A;
B: B;
}
const utils: { [K in keyof UtilMap]: CommonUtilFns<UtilMap[K]> } = {
A: {
isAorB: (version: Common): version is A => !!(version as A).propA,
getProp: (version: A) => ({ prop: version.propA }),
},
B: {
isAorB: (version: Common): version is B => !!(version as B).propB,
getProp: (version: B) => ({ prop: version.propB }),
}
};
function apply<K extends keyof UtilMap>(util: (typeof utils)[K], val: Common) {
return util.isAorB(val) ? util.getProp(val) : undefined
}
{
const getProp = (val: Common) => {
for (const util of Object.values(utils)) {
const ret = apply(util, val);
if (ret) return ret;
}
return null;
};
} Which, yes, is a lot of weird generic stuff. See #47109. Anyway I'd say this is essentially a duplicate of #30581. |
Thanks for the detailed explanation @jcalz! Your I'm going to close this issue. Maintainers, feel free to mark it as a duplicate. And thanks again for your time Joe 👍 |
🔎 Search Terms
"generic narrowing", "union narrowing", "generic guard", "ts2345"
🕗 Version & Regression Information
v3.9
tov5.5.0-dev.20240316
), and I reviewed the FAQ for entries about type narrowingProperty 'values' does not exist on type 'ObjectConstructor'.(2339)
⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.5.0-dev.20240316#code/MYGwhgzhAEDCD2BbR8B20DeBfAUKSMAgtAKYAeALiagCYwLJqY4CQADgK4BGIAlsNABOJMDTQgAntDaD4bYgF5oAImUBuHLnxRoAIVKVqdOEhToMrTj35CRY1JOmy2+pao24cFCWxInGqACqFLwgAGKoEAA8ACoGVLT0pmgAfNBKFiy8EITwgroAXNAAFABuYCBFDGYAlOlp5SDQ2dAxGiwA5iQUAArORWUVRTF1CmkYTnJFEBSCvKgd0FgeGnhoM9AcISAwGayERZlZOXmFJaUkghC8aFXJqDVFF1c36C2KaQCEn2WX10yQaCEGoAOhkckIABpWJ1un0puc-q8isD6iUJuC2E8kWgwc5iFgatCWFhoBAwCEIAAzXgkJIBYKhCLRQgpYlnI7ZXL5AbPf6oO4BR7QPmvZowVxfH6igES0GY3TE2G9fqIl63PSjNLFDGqmWoPFyfSE4mk8mUml0-xmRnhSJRXRszSrTLAdYUaBdFVydLnIbWtBa5gsFhUvIlN2RD1bULQeBU6AAeS4ACsSMAKCDGhw6cUYzsanUjlkE3ntiCuadBiBC7YKBxBOh8yCvfC2NWau0STDhPXG9BUBwQCB2stNDgcK73Z64c5fdXBWZIZtthBF2hbcyotVUgBtAC6QaOYcEEen+bjCfzEDqvFLzcr+Q7dV7Dab5dbzg7Xdf-cHw9HDwJynKMZ29Nh50addUGXa9oM3e1iAAHz0FIDyPGETzPUCL3jFdQhvZp73LR9dGfOs33wkAW1nORvx7bpKP-EdWDHXAgA
💻 Code
🙁 Actual behavior
Invoking
util.getProp
after guarding it withutil.isAorB
results in a type error:The return value of
Object.values
seems to be correctly typed, as this is the evaluated typing ofutil
:However, once
val
is guarded, its type becomesA | B
🙂 Expected behavior
I would expect the type guard to correctly narrow the type as
A | B
, given I'm using the same instance ofCommonUtilFns
with the same generic.This however works correctly with these examples:
Additional information about the issue
This might be related to #17713, but I haven't found another issue specifically addressing this pattern, apologies if I missed it
The text was updated successfully, but these errors were encountered: