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
typePropsNotShared<LHSextends{},RHSextends{}>=Omit<RHS,keyofLHS>&Omit<LHS,keyofRHS>;exportfunctionhandlePropsNotShared<LHSextends{},RHSextends{}>(lhs: LHS,rhs: RHS){/** * [====================================] * Set everything up first: * [====================================] *//** * Assertion because it's just for checking types. */constnotShared={}asPropsNotShared<LHS,RHS>;/** * ts says the type of keyofNotShared is: * * ```Exclude<keyof RHS, keyof LHS> | Exclude<keyof LHS, keyof RHS>``` * * Which makes sense, given @see PropsNotShared */constkeyofNotShared={}askeyoftypeofnotShared;constkeyofNotShared_firstSet={}asExclude<keyofRHS,keyofLHS>;constkeyofNotShared_secondSet={}asExclude<keyofLHS,keyofRHS>;/** * Check that the above two types are indeed keys of notShared. Succeeds * because they are. */constcheckKeyofNotShared_firstSet: keyoftypeofnotShared={}astypeofkeyofNotShared_firstSet;constcheckKeyofNotShared_secondSet: keyoftypeofnotShared={}astypeofkeyofNotShared_secondSet;/** * Expected failures to illustrate that `keyofNotShared_firstSet` and * `keyofNotShared_secondSet` are not identical (not assignable to each * other). */constcheckKeySetsNotSame_1: typeofkeyofNotShared_firstSet={}astypeofkeyofNotShared_secondSet;constcheckKeySetsNotSame_2: typeofkeyofNotShared_secondSet={}astypeofkeyofNotShared_firstSet;/** * Some sets of values from notShared. */constnotSharedVals_firstSet={}astypeofnotShared[typeofkeyofNotShared_firstSet];constnotSharedVals_secondSet={}astypeofnotShared[typeofkeyofNotShared_secondSet];/** * Expected failures to show that `notSharedVals_firstSet` and * `notSharedVals_secondSet` are not identical. */constcheckValSetsNotSame_1: typeofnotSharedVals_firstSet={}astypeofnotSharedVals_secondSet;constcheckValSetsNotSame_2: typeofnotSharedVals_secondSet={}astypeofnotSharedVals_firstSet;/** * [====================================] * And now the main problems: * [====================================] *//** * (1) * * Should succeed but doesn't. * * Shouldn't a union of some values from notShared (rhs) be assignable to: * `typeof notShared[keyof typeof notShared]` (lhs) ? */constx1: typeofnotShared[keyoftypeofnotShared]={}as|typeofnotSharedVals_firstSet|typeofnotSharedVals_secondSet;/** * (2) * * (This is the issue referenced in title): * * Should fail, I would think. * * How is an interection of some of the values from an object (rhs) * assignable to a type which is any value of that object (lhs) ? * * (when the types of those values are not identical, * And they aren't according to above lines involving * `checkValSetsNotSame_1` and `checkValSetsNotSame_1`.) */constx2: typeofnotShared[keyoftypeofnotShared]={}astypeofnotSharedVals_firstSet&typeofnotSharedVals_secondSet;/** * (3) * * More weirdness: * * Should fail but doesn't. * * I would think, as doesn't it basically say A | B == A & B ? * (and A and B aren't identical) */constx3: typeofnotSharedVals_firstSet|typeofnotSharedVals_secondSet={}astypeofnotSharedVals_firstSet&typeofnotSharedVals_secondSet;}
π Actual behavior
Given a generic object produced via an intersection, a union of it's values is not assignable to Obj[keyof Obj] (i.e. any value of it) (example 1 in comments), but an intersection is and shouldn't be (example 2 and in title).
π Expected behavior
Assigning a union of it's values to Obj[keyof Obj] should succeed (example 1) and assigning an intersection of them should (I suppose) fail (example 2).
The text was updated successfully, but these errors were encountered:
Hmm actually I may have made a mistake here. This helpful comment explained some things, and they make sense as to why doing T[K] is on the target side of a type relationship is equivalent to an intersection of all the value types of T.
We know keyof (A & B) is (keyof A) | (keyof B), and T[M | N] is T[M] | T[N], so T3[keyof T3] is resolved to T3[keyof T] | T3[keyof U] as expected, when it is in a "read position".
However, when T3[keyof T3] appears in the "write position", it uses an intersection instead of a union, so T3[keyof T3] is resolved to T3[keyof T] & T3[keyof U], which leads to the error.
Bug Report
π Search Terms
indexing intersections, generic object indexing
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Given a generic object produced via an intersection, a union of it's values is not assignable to
Obj[keyof Obj]
(i.e. any value of it) (example 1 in comments), but an intersection is and shouldn't be (example 2 and in title).π Expected behavior
Assigning a union of it's values to
Obj[keyof Obj]
should succeed (example 1) and assigning an intersection of them should (I suppose) fail (example 2).The text was updated successfully, but these errors were encountered: