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
This changed between versions 4.8.0-dev.20220613 and 4.8.0-dev.20220614. I noticed the issue today, When VSCode updated to version 1.71.0 and upgraded to TypeScript 4.8.2. The nightly build on TypeScript Playground does not have the issue, but the latest version of 4.8 does.
interfaceBaz{foo: {a: number};}constcreateDefaultExample=<KextendskeyofBaz>(x: K): Baz[K]&{x: K;}=>{return{a: 0, x };// okay in TS4.7, error in TS4.8}
🙁 Actual behavior
TypeScript is flagging previously valid code as having error 2322. I believe this to be incorrect because this change is not part of any release notes and seems like an unintentional bug. TypeScript seems to be ignoring part of the intersection type when checking if the returned value can be assigned to the return type of the function. This is the error text:
Type '{ a: number; x: K; }' is not assignable to type 'Baz[K] & { x: K; }'.
Type '{ a: number; x: K; }' is not assignable to type 'Baz[K]'.
Type '{ a: number; x: K; }' is not assignable to type '{ a: number; }'.
Object literal may only specify known properties, and 'x' does not exist in type '{ a: number; }'.
🙂 Expected behavior
The code to not be flagged as having error 2322, since it seems like there is something wrong with the way TypeScript is resolving intersection types possibly in conjunction with generics.
As I mentioned it looks like it was caused by #49503, and it might have been fixed by #50261? There are other formulations with intersections that still tickle this, though.
Intersection, not union. Just wanted to get that out of the way.
That’s interesting, it seems to be doing excess property checking on the individual components of an intersection. x is definitely not an excess property with respect to something intersected with { x: K }
functionSendBlob(data: unknown,encoding: unknown){if(encoding!==undefined&&encoding!=='utf8'){thrownewError('encoding');}// Mouse hover to see detected type // When using TS v4.8.2 it is {} | undefined// When using TS v4.7.4 it is "utf8" | undefinedencoding;};
I don’t think that’s the same bug. In this issue an assignability check is going wrong but the types are otherwise correct, yours is just an outright failure to narrow to the correct type. You should open a new issue.
Activity
jcalz commentedon Sep 2, 2022
As I mentioned it looks like it was caused by #49503, and it might have been fixed by #50261? There are other formulations with intersections that still tickle this, though.
fatcerberus commentedon Sep 2, 2022
Intersection, not union. Just wanted to get that out of the way.
That’s interesting, it seems to be doing excess property checking on the individual components of an intersection.
xis definitely not an excess property with respect to something intersected with{ x: K }Matt23488 commentedon Sep 2, 2022
Good catch. Fixed.
lll000111 commentedon Sep 9, 2022
Not only unions. Here is a simpler example with the most basic types and no unions or intersection types:
https://www.typescriptlang.org/play?ts=4.8.2#code/GYVwdgxgLglg9mABAZQKZgCYCEA2cBGAFBgIZQkBci4A1mHAO5gA0i6EcGMYA5lbfSYBKRAG8AsAChEMxDGCJC7Ttx6IAhAF5N1TKmDdUGRADITbSCt4btiAOQgowABx2RE6bK9QAFgCdGRDBUBkQAUT8Av0I7ZS5eNwBuKS8AXykU2QB6LMQAWTgQAGdURB84ADdUP0QoOEQS0oxUKFRoI1qATwAHUsyZHMQAdR90aiLVRAAVZEQKgBYAOmdFgCY5KDkisVTEAB9dZoNgjH7EQZGx4smZuaWAdkX5ja3EACJHFzf9w-1DU88MjiqmSklSoKAA
fatcerberus commentedon Sep 9, 2022
I don’t think that’s the same bug. In this issue an assignability check is going wrong but the types are otherwise correct, yours is just an outright failure to narrow to the correct type. You should open a new issue.
andrewbranch commentedon Sep 13, 2022
Fixed in 4.8.3