Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected error 2322 in TypeScript 4.8.2 #50616

Closed
Matt23488 opened this issue Sep 2, 2022 · 6 comments
Closed

Unexpected error 2322 in TypeScript 4.8.2 #50616

Matt23488 opened this issue Sep 2, 2022 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Matt23488
Copy link

Matt23488 commented Sep 2, 2022

Bug Report

🔎 Search Terms

  • typescript 4.8 error 2322 generic indexed type

🕗 Version & Regression Information

  • 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.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Baz {
  foo: { a: number };
}

const createDefaultExample = <K extends keyof Baz>(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.
@jcalz
Copy link
Contributor

jcalz commented 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
Copy link

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 }

@Matt23488
Copy link
Author

Intersection, not union. Just wanted to get that out of the way.

Good catch. Fixed.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 2, 2022
@lll000111
Copy link

lll000111 commented 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

function SendBlob(data: unknown, encoding: unknown) {
    if (encoding !== undefined && encoding !== 'utf8') {
        throw new Error('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" | undefined
    encoding;
};

@fatcerberus
Copy link

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 andrewbranch added the Fixed A PR has been merged for this issue label Sep 13, 2022
@andrewbranch
Copy link
Member

Fixed in 4.8.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants