Closed
Description
Bug Report
π Search Terms
generic, function, parameters, unknown, never
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
Generics
β― Playground Link
Playground link with relevant code
π» Code
interface Entity {
id: number;
name: string;
}
type Fn<T> = (params: Record<string, unknown>) => T[];
declare function test<T, K extends keyof T>(fn: Fn<T>, key: K): T;
// Case #1: params aren't used, generic is `<Entity, 'id'>` β
test(() => [] as Entity[], 'id');
// Case #2: params are used with a type being explicitly specified, generic is `<Entity, 'name'>` β
test((params: Record<string, unknown>) => [] as Entity[], 'name');
// Case #3: params are used with a different type, generic is `<Entity, 'id'>` β
test((params: unknown) => [] as Entity[], 'id');
// Case #4: params are used with an implicit type, generic is <unknown, never> β
test((params) => [] as Entity[], 'id');
^ but it'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.
Metadata
Metadata
Assignees
Labels
No labels