Inconsistent TS2345 error when using function generics
See original GitHub issueBug 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:
- Created a year ago
- Comments:10 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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 instantiateT
.