Inconsistent TS2345 error when using function generics
moshest opened this issue Β· 18 comments
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
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
.
@fatcerberus you are right, I've updated the issue description.
This looks similar to some other bugs we've seen reported where weird errors show up while typing
I also encounter the similar situation when passing function paramter in different ways.(typescipt version 4.1.3)
interface AAA {
hyYears: Array<any>;
}
function func(process: AAA) {
console.log(process);
}
const unresolveParams = {
hyYears: [],
periodDataId: 1,
};
func(unresolveParams); // why this line can't detect the extra parameter
func({
hyYears: [],
periodDataId: 1, // ts(2345) detect the extra parameter. Argument of type '{ hyYears: undefined[]; periodDataId: number; func(unresolveParams: any): any; }' is not assignable to parameter of type 'AAA'
});
@JonathonChen your case is not a bug, you can learn more about this here
@Andarist How did you happen to reproduce this? Just using the editor or did you manage to get a test working? (I so far have not...)
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
Darn π
I haven't yet found the right fourslash calls to get this reproducing, so into the server RPC logs I go...
Got it:
////declare function func<T extends { foo: 1 }>(arg: T): void;
////
////func({ foo: 1, bar/*1*/: 1 });
goTo.marker("1");
edit.insert("2");
verify.completions({ exact: undefined });
verify.noErrors();
(I spent all that time figuring out how to assert in fourslash that there are no completions so I could perform the request.)
You may interested in a similar issue I filed in the realm of "errors that only show up while typing": #51661
I don't know if it's the same underlying cause, or if my brief investigation into it was way off the mark or not, but food for thought.
Amazing! π
I can confirm it resolved on nightly build using the playground link.
I ran into similar issue, initially with generics, but the simplified to even without generics. I am still not sure what was causing inconsistencies in different situations, but I did find that having a property with the name "name" seemed to cause the error reporting, I dont know if word is reserved or something, either way thought would share if its useful at all https://www.typescriptlang.org/play?#code/FAFwngDgpgBAsmA8gIwFZQMYhgXhgb2GBhgCcoBDAEwHsA7AGzBgDcKGBXKAZwC4DiJMpVqNmdCgFso-biFIBLOgHMA3IIC+6rUQz05MACIBBACrH+CFOiy4BJNpx79CQmBOn8ARI65eANJqBOrr62NxQIADCFNg4ggAUvjLwSGiYIADaPuxc3F4AugCUuAB8iRR0CpLsfKnWGUWWaTZxpTAJrkIAdL2V1bWBbsl1yUMwGkXqwHp0BgDuNKQA1tx2EdGxnR4yXnRQ8zAYsV6ToXPhABY0HAxUABIULFAAoqSkS+uRMSAJCSU4dr-ModAFA-DuKQpPYHGAANVyUC8EyKRQSJnMUyIQA
I see no error in the above snippet, so if you're having a problem and can reproduce it without generics, I would suggest making a new issue as that's very likely different.
Great thank you, before I create an issue I would appreciate if you could take a glance at
It's a little clearer than the above one but same thing. The fact there is no error is the issue as I understand, it should not accept function, but seems to when field is called 'name', just to make sure its not something already known. Thanks
Nice, thanks for clarifying, feel free to hide or delete my posts if useful, cheers.