Skip to content

Type inference error when using type parameters as constraints #8411

@spikefoo

Description

@spikefoo

When compiling the following code: (the function call takes a 0-argument function and returns its result)

type NoArgsFn<T> = () => T;

function call<T, Fn extends NoArgsFn<T>>(fn: Fn): T {
  return fn();
}

let result: number = call(() => 1);

I get the following incorrect error: (on the last line)

try.ts(7,5): error TS2322: Type '{}' is not assignable to type 'number'.

The following workaround, adding an unused dummy argument of type T to the function, avoids this error:

type NoArgsFn<T> = () => T;

function call<T, Fn extends NoArgsFn<T>>(fn: Fn, dummy: T): T {
  return fn();
}

let result: number = call(() => 1, -1);

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions