Type matching inside of nested mapped type causes error on assignment, but otherwise TS knows the right type #51117
Labels
Working as Intended
The behavior described is the intended behavior; this is not a bug
Bug Report
π Search Terms
typescript two level mapped type inference error
Possible match? #23897
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBA6glsAFgBQPYGd1wEYBsLIBOqkhoA0gPIBmAKuBLQDzlQQAewEAdgCbpR0wQnG4BzADRRaAPigBeKAG8AUFHVQA2siiio5ALoB+AFzSA3CoC+llQGNU3IVDsBDO4ggARV8FcKoJloAZQgHPgAZCAA3CFw2Th5+WAQUDCw8AmJSChp6SGZyKSERcRkpVg4uPgES0TEZAAo1DQBrCBAaAGF3TwBJblDw3ijY3DMilvU7AFdCQh5gHo8IMwAlMNRCXiY68SkQzciYuJkASimoM2jUOF4FOVUNKAB6F6gAUXmts3zoAHI9mJ-roBNxUMAoK5MHAxNxXJkoMBUEiGFB-odhqM4ppDP8AHSXWbzRbLTyaQGoAC2ED6vH+Bk07U61DJtMGRxGJ1wBgC-1c-1sz3Czg4cCE9QCxIW3CWvQgFPQ1Np9MZzO68oGQ0cXLGBiFGjeUGCMzsdggmGoM1wuBAUAWrlwcAAXhakYhfKjIKCoYJhPVLmKJeIjPiPK5CABBYCNABMZ0sNiAA
π» Code
π Actual behavior
Type 'string' is not assignable to type 'TSecondLevel[K]'.
π Expected behavior
No error.
The type I'm using seems a tiny bit contrived, but the scenario is that I'm inside of a reducer that is later reduced with other reducers to produce a slice reducer.
The overall state slice looks like the following:
The reducer I'm working on takes the entire state slice
users
and the key of that cached property in the second level and it needs to specifically handle updating the same property, (in the above example,cachedUserProperty
). Other reducers handle updating the other properties inside the specific user, and the several reducers are reduced together to produce the slice reducer. My reducer is generically written, because I have several such structures in my state, with different types and keys in the second level. (Actually the reducer is produced by a reducer producer to achieve this, but that's too much detail).The type that you see in my repro, WithPossiblePropertyKOfTypeT, allows me to model the fact that the second level of my data (ie,
users[string]
) has a property with keyK
of typeT | undefined
.In the repro, notice that when I try to assign string to that property, it errors out. But TS knows that that is a string, because it gives no errors when I try to access string functions on an already-existing value in that property.
The full repro has the type of that property be a more complex object, rather than string, and TS even knows the types inside that object if I assign it to a variable and then try to access other properties inside of it.
So TS knows what the type is, but it's erroring out when trying to equate it to
TSecondLevel[K]
.The text was updated successfully, but these errors were encountered: