You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
generic, keyof, constrained, mapped type, conditional type
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "This conditional type returns never when it should return the true branch"
I'm not sure, in my opinion the first one (Ft) seems reasonable. But one thing is clear. Their result should be same.
Additional information about the issue
I suspected G<T, K> doesn't count external condition. I think G<T, K> just consider T[keyof T] undecidable, so that just fallb back to falsy type. But I'm not sure why it didn't happend with unconstrained one(F<T, K>)- which conditionally branched.
The text was updated successfully, but these errors were encountered:
The first is a distributive conditional type. Check the documentation about conditional types. This is unrelated to keyof.
In your G case T[K] results in a union of all property types, so string | number, and this does not extend string, so you end up with false.
In your F case you have a distributive conditional type over K, so you run the conditional type for each property type and end up with a union. So basically you have (string extends string ? true : false) | (number extends string ? true : false). This type resolves to true | false and then is simplified to boolean.
Thank you for your reply. I think I misunderstood some concept of distributive conditional logic. I'll check it out once more. I'll close this as completed.
🔎 Search Terms
generic, keyof, constrained, mapped type, conditional type
🕗 Version & Regression Information
Extract
not narrowing type union in mapped type passed as generic #57827 Generic constraint wipes out precise key type #56912⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.4.5#code/C4TwDgpgBAYgPAFQDRQNID4oF41QgD2AgDsATAZygGsIQB7AMygSgH5mBtVAXT0JIpRywAE4BLYgHM2UUQFdoALigMAhgBtySqMQgA3CCIBQoSFADiiFKj5EylGvSYJMOBF14E7g4eKkz5bTVNCCMTcGgWHABvKHxlYjkAWwAjQxQQZV8JaQBfMNNoGGBsWCtqWkZmTAB6GqgUujp1CFVicLNzEpxLZAqnaqg6lQ0tIA
💻 Code
🙁 Actual behavior
Ft
isboolean
whileGt
isfalse
🙂 Expected behavior
I'm not sure, in my opinion the first one (
Ft
) seems reasonable. But one thing is clear. Their result should be same.Additional information about the issue
I suspected
G<T, K>
doesn't count external condition. I thinkG<T, K>
just considerT[keyof T]
undecidable, so that just fallb back to falsy type. But I'm not sure why it didn't happend with unconstrained one(F<T, K>
)- which conditionally branched.The text was updated successfully, but these errors were encountered: