Skip to content

Inconsistent TS2345 error when using function genericsΒ #50818

Closed
@moshest

Description

@moshest

Bug Report

πŸ”Ž Search Terms

  • Inconsistent 2345
  • function generics
  • extends 2345
  • generics 2345

πŸ•— Version & Regression Information

  • This changed from version 4.7.4+ and above

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function func<T extends { foo: 1 }>(arg: T): void {}

func({ foo: 1, bar: 1 });

πŸ™ Actual behavior

bar property sometimes displayed as valid and sometimes produce an error:

Argument of type '{ foo: 1; bar: number; }' is not assignable to parameter of type '{ foo: 1; }'.
  Object literal may only specify known properties, and 'bar' does not exist in type '{ foo: 1; }'.(2345)

When playing with the code live and change bar to bar222 or ba sometimes the error shown and sometimes not.
Refreshing the playground link with the changes always remove the error.

Screen.Recording.2022-09-17.at.20.20.57.mov

πŸ™‚ Expected behavior

Always display as valid without any error.

{ foo: 1, bar: 1 } is a valid subtype of { foo: 1 } and so can legally be used to instantiate T.


Maybe related to #49490

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions