question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent TS2345 error when using function generics

See original GitHub issue

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.

https://user-images.githubusercontent.com/534911/190869020-376ff387-56ff-48b3-83e4-43261b2b4d78.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 https://github.com/microsoft/TypeScript/issues/49490

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Andaristcommented, Oct 13, 2022

I was using the editor with TS built from different commits. I was doing that within the TS repo itself so I could pretty easily jump between commits/builds

1reaction
fatcerberuscommented, Sep 17, 2022

Expected behavior: Always display an error for unknown properties.

The correct behavior is for there to be no error here: excess property checks aren’t intended to apply to generic constraints. { foo: 1, bar: 1 } is a valid subtype of { foo: 1 } and so can legally be used to instantiate T.

Read more comments on GitHub >

github_iconTop Results From Across the Web

basarat on Twitter: "@hmans This should not have been an error ...
PS: it is not an error in TS 4.6 (error in 4.7+) /cc @mikeysee " / Twitter ... Inconsistent TS2345 error when using...
Read more >
error TS2345: Argument of type 'T' is not assignable to ...
Change signature of the function, so that generic type T extends type object , introduced in Typescript 2.2. Use this syntax - <T...
Read more >
TypeScript: Generics - Robin Wieruch
How to use generics in TypeScript. You will learn how to create a arrow function which uses a generic type to infer the...
Read more >
A complete guide to TypeScript's never type - Zhenghao
Inadmissible parameters in generics and functions. Intersection of incompatible types. An empty union (a union type of nothingness).
Read more >
How To Use Generics in TypeScript - DigitalOcean
Returning to the fun function, use T to make your generic function ... Unable to compile TypeScript: index.ts:53:25 - error TS2345: Argument ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found