Skip to content

Unexpected error 2322 in TypeScript 4.8.2 #50616

Closed

Description

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.

Activity

  1. jcalz commented on Sep 2, 2022

    @jcalz
    Contributor

    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.

  2. fatcerberus commented on Sep 2, 2022

    @fatcerberus

    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 }

  3. Matt23488 commented on Sep 2, 2022

    @Matt23488
    Author

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

    Good catch. Fixed.

  4. lll000111 commented on Sep 9, 2022

    @lll000111

    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;
    };
  5. fatcerberus commented on Sep 9, 2022

    @fatcerberus

    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.

  6. andrewbranch commented on Sep 13, 2022

    @andrewbranch
    Member

    Fixed in 4.8.3

  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions