Skip to content

Generics get broken when inferred from a function with an implicitly typed parameterΒ #54438

Closed
@mattersj

Description

@mattersj

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions