-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistent behavior when inferring a generic type from a function #52580
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
Aside: The contravariant-only inference on |
@RyanCavanaugh - Thanks for the response. The real code is quite involved; I've tried to distill it down. The use case is a React H.O.C. which accepts a struct describing its remote data dependencies. const WithMentorData = remoteDataDependencies({
mentee: {
decoder: personDecoder,
src: (menteeId: string) => u`/people/${menteeId}`,
},
mentor: {
decoder: personDecoder,
src: (_menteeId: string, data) => {
if (data.mentee.state === 'ok') {
const mentee = data.mentee.value;
const mentorId = mentee.mentor;
return u`/people/${mentorId}`;
}
},
},
}); For each dependency, <WithMentorData
arg={menteeId}
render={data =>
<MentorProfile
menteeData={data.mentee}
mentorData={data.mentor}
/>
}
/> My code mostly works. Property names (e.g. |
This looks quite interesting, gonna try to dive into this at some point - unless somebody beats me to it. |
[edit] Looks like what I'm describing in this comment may be a known/separate issue. Here's another oddity involving the same error message, so seems to be related. I've attached a screen recording because I can only trigger it by live-editing the code. Here's the sequence:
Please ignore if this is a separate issue and/or unrelated. Screen recording Screen.Recording.2023-02-03.at.9.36.29.AM.mov |
This one is likely the same as the one reported here. I'm working on fixing that issue here. I'll try to verify later if it fixes your issue as well. EDIT:// well, the other one is related to property excess checks - it's unlikely that your case is related to that. Still, it's likely related to some errors being incorrectly cached somewhere and we were discussing a more general approach to fixing that. I will try to create a test case reproducing your issue - it might help us to create a better fix. |
I prepared a potential fix for this issue: #52597 As a workaround in the meantime you might want to prefer regular functions over arrows, it works with them: TS playground |
I reproduced the issue with incorrect error messages using this test: ///<reference path="fourslash.ts"/>
// @strict: true
////
//// type Funcs<A, B extends Record<string, unknown>> = {
//// [K in keyof B]: {
//// fn: (a: A, b: B) => void;
//// thing: B[K];
//// }
//// }
////
//// function foo<A, B extends Record<string, unknown>>(fns: Funcs<A, B>) {}
////
//// foo({
//// bar: { fn: (a: string, b) => {}, thing: "asd" },
//// /*1*/
//// });
goTo.marker("1");
const markerPosition = test.markers()[0].position;
edit.paste(`bar: { fn: (a: string, b) => {}, thing: "asd" },`)
edit.replace(markerPosition + 4, 1, 'z')
verify.completions({ isNewIdentifierLocation: true });
verify.noErrors(); Now I need to dive deeper into this to understand why this is happening π |
Bug Report
π Search Terms
Not sure what to search for, tried various combos of "infer" "generic" "function" but seems too general.
π Version & Regression Information
Tried in 4.0.5, 4.6.3, and 4.9.4.
β― Playground Link
Playground link with relevant code
π» Code
Actual behavior
It infers generic
<A>
asunknown
, in the former case only. It then checks assignability ofunknown
toa: string
and raises an error.Expected behavior
I didn't expect it to infer
unknown
in the former case, and more generally was surprised that including/excludingb
from the argument list would affect inference froma
. I thought it should seestring
in both cases, and that all of the above would compile.The text was updated successfully, but these errors were encountered: