You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceEntity{id: number;name: string;}typeFn<T>=(params: Record<string,unknown>)=>T[];declarefunctiontest<T,KextendskeyofT>(fn: Fn<T>,key: K): T;// Case #1: params aren't used, generic is `<Entity, 'id'>` β test(()=>[]asEntity[],'id');// Case #2: params are used with a type being explicitly specified, generic is `<Entity, 'name'>` β test((params: Record<string,unknown>)=>[]asEntity[],'name');// Case #3: params are used with a different type, generic is `<Entity, 'id'>` β test((params: unknown)=>[]asEntity[],'id');// Case #4: params are used with an implicit type, generic is <unknown, never> βtest((params)=>[]asEntity[],'id');^butit's inferred properly as Record<string, unknown>
π Actual behavior
In the case 4 a generic gets broken and has a type <unknown, never> instead of <Entity, 'id'> as shown in the previous examples.
There's nothing wrong with an implicit type for params since it's already automatically inferred as Record<string, unknown>.
π Expected behavior
The case 4 should work exactly the same as the previous ones and missing type annotation in parameters should have no impact on the inferred generic type.
The text was updated successfully, but these errors were encountered:
Duplicates the part of #47599 that's still unresolved... and might never be resolved, see #48538 (comment):
() => 42 and (n: number) => n are context insensitive because they have no contextually typed parameters, but n => n and function() { return 42 } are context sensitive because they have at least one contextually typed parameter (in the function expression case, the implicit this parameter is contextually typed). The errors in the last two calls occur because inferred type information only flows from left to right between context sensitive arguments. This is a long standing limitation of our inference algorithm, and one that isn't likely to change. [emphasis added]
Thanks @jcalz, I suspected this was related to contextual typing but wasn't confident enough about the cause to jump in. I thought it might just be a circularity (i.e. T infers from function parameter -> arrow function is contextually typed by T -> cycle detected -> default to constraint)
@jcalz Thank you for clearing this up, it makes sense now. I'm going to close this one as it already has a bunch of duplicates out there.
So, for anyone coming up here the best solution is to explicitly add a type annotation for your parameter like this: (params: YourType) => ... or get rid of your parameter at all.
Bug Report
π Search Terms
generic, function, parameters, unknown, never
π Version & Regression Information
Generics
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
In the case 4 a generic gets broken and has a type
<unknown, never>
instead of<Entity, 'id'>
as shown in the previous examples.There's nothing wrong with an implicit type for
params
since it's already automatically inferred asRecord<string, unknown>
.π Expected behavior
The case 4 should work exactly the same as the previous ones and missing type annotation in parameters should have no impact on the inferred generic type.
The text was updated successfully, but these errors were encountered: