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
Maybe related to #48626 (comment)? I don't fully understand the comment.
csr632
changed the title
Two same types (generic function) is not assignable to each other
Two same types (generic function) are not assignable to each other
Jun 23, 2022
csr632
changed the title
Two same types (generic function) are not assignable to each other
Two same types (mapped type of generic param) are not assignable to each other
Jun 23, 2022
The issue here is that we only consider two mapped types with as clauses related if the as clause types are identical. We could probably be a bit more flexible here. Meanwhile, the workaround is to use a shared as type:
typeN<K>='test'extendsK ? never : K;typeF1=<T>(arg: T)=>R1<T>;typeR1<T>={[KinkeyofTasN<K>]: 0;};typeF2=<T>(arg: T)=>R2<T>;typeR2<T>={[KinkeyofTasN<K>]: 0;};
I am running into the same issue and found a slightly simpler example. Link to playground.
typeR1=<TextendsPropertyKey>()=>{[KinTasKextendsunknown ? K : K]: 0}typeR2=<TextendsPropertyKey>()=>{[KinTasKextendsunknown ? K : K]: 0}declareletv1: R1;declareletv2: R2;v1=v2;// Errorv2=v1;// Error
Type 'R2' is not assignable to type 'R1'.
Type '{ [K in T as K extends unknown ? K : K]: 0; }' is not assignable to type '{ [K in T as K extends unknown ? K : K]: 0; }'.
Two different types with this name exist, but they are unrelated.
Removing as K extends unknown ? K : K (even though this clause is a noop) unexpectedly removes that error.
Unfortunately, in my case, the workaround described by @ahejlsberg is not possible because R1 and R2 are actually two instances of the same package installed in different places in node_modules. This leads to my package's types not being assignable with themselves (or more precisely, a copy of themselves).
Bug Report
🔎 Search Terms
Keywords: generic function, mapped types
Errors:
I got both error messages in my original code, but only the first error message is left in the minimal reproduce.
🕗 Version & Regression Information
4.7.2
All other versions also have this issue.
⏯ Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgjFAvFAPAFQHwAoCGAnAcwC4o0BKJDKAJTnQwG4AoUSGuzJKAbyaigDaAaSgBLAHZQA1hBAB7AGakoOAM5QA5MAirgGqBAAe28QBN1IgPxRxEAG4Q8UEkIC6JAAzMAvsyYB6fwNDHABjYAAbEChgAAtoVRwAW2hWCBZwaBgAJi56XEISckoabPpmNNL6Ll5+YTFJGXklNBV1LR09YJNzKCsbe0dnPvcoLyZfJiZTCFCI-GgIiGAoOzgSeGYZuYWoJZW7bI3svzWuQ4YoQKgAUTw8OTwmQ-O4S+u7h6emIA
💻 Code
The problem appears when a conditional type is used in a key remapping of mapped types.
🙁 Actual behavior
The two same types are not assignable to each other.
🙂 Expected behavior
The two same types should be assignable to each other. Because they are written in the same way.
In my case, the two "same-written type declarations" exist because they come from the same package with two different versions.
The text was updated successfully, but these errors were encountered: