Skip to content

Unexpected error 2322 in TypeScript 4.8.2 #50616

Closed
@Matt23488

Description

@Matt23488

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.

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