-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Surprising excess property check with recursive generic constraint #55644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this is actually a weak type assignability thing being reported as an excess property check thing after inference fails. You can see the common properties check by moving the assignment out to a variable declaration, and you can also see make it work in inference by adding a I think the bug here is that the error message is wrong. |
This might be the case, I see that - indeed - the excess property check fails in |
What youβre asking is whether this code should be error-free: interface Options {
x?: boolean;
y?: string;
z?: number;
}
declare function f<T extends { options?: boolean }>(obj: T): T;
f({
opitons: {
x: true,
y: "hello",
z: 42
}
}) It feels to me like the mistake that common property checks are intended to catch is just as relevant in inference as in other assignability checks. |
Yee, so I guess what I'm most focusing on here is the recursive aspect of my constraint. I know though that distinguishing those situations might be rather hard. This is just the most reduced repro case of my problems. On its own, I don't mind this as much. I created a linked issue to this one that shows better why this is a problem. The intersection thing there changes everything though - it's just that the root problem is similar. The I think though that some issue has been identified here - the error is somewhat confusing here. It doesn't report why this failed in practice. Inferred object types are exempt from the excess property checks because they are widened in I tried reordering the excess property check with the common property check but that didn't pan out. The common property check embeds rename suggestions and stuff like that. Those are much more helpful than common property errors and thus they should be prioritized in simple situations. So my best ideas to fix this are:
|
After a second consideration, maybe the error message shouldn't even get fixed here. See my comment here. If we think about inferred type arguments as "assignments" then yes - this should very much fail at that stage. The error message is somewhat surprising because that assignment is just hidden from us so we don't see it. The error that is reported today isn't incorrect - it just feels off a little bit. If we disassociate argument type checking from the implicit type argument assignments... then it stop being so weird π
What I'm saying, is perhaps it just doesn't matter why the inferred candidate was discarded. All that matters is the relation between what got finally inferred (the |
π Search Terms
excess property check recursive generic contraint type variable inference
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.0-dev.20230906#code/C4TwDgpgBAysCGwIwMYAsIFt5QLxQG8AoKKAZwSTIH4AuKAJQhQHsAnAEwB4K2BLAHYBzADSxKydFngA+ANxEAvgqIdmAG3htoAMwCuAlMD4sBUFNsQQAsvHSCIXACoBhUzr5CoEAB5IBHGTiVqgY2DIAFCTm7p70rrGiRACU8W4CHkIqFhBWtvYCEBHEpBACwGwg9ADaBFCgkPQARDosLE1QigC6ItEUVmT0JaRQ8EPRI97llTV1DRDNAEZaHd29k4rrnb2KyQpAA
π» Code
π Actual behavior
At the nested
entry
property the error is raised:but it isn't raised at the
entry
property at the root of this object.π Expected behavior
I'd expect no excess property error to be raised with a recursive constraint like this.
Additional information about the issue
This particular case is somewhat easily fixable by adding
[k: string]: unknown
toStateSchema
. I still find the reported behavior to be a problem though.The text was updated successfully, but these errors were encountered: