TypeScript generic type and default function application

I have the following TS code:

export interface Id<T> {
  (arg: T): T;
}

export function IdFunc<T>(arg: T): T {
  return arg;
}

I want to use the Id and IdFunc like so to provide a default implementation of transformer.

interface Options {
  transformer: Id<number>;
}

const defaultOptions : Options = {
  transformer: IdFunc<number>
               ^^^ Type 'number' is not assignable to type 'Id<number>'
}

But I get an error when doing this, is there a way to accomplish using function name + type for the default option without doing something like this:

const myIdentity: Id<number> = IdFunc;

const defaultOptions : Options = {
  transformer: myIdentity
}

Playground link

6 thoughts on “TypeScript generic type and default function application”

  1. Fantastic goods from you, man. I’ve take note your stuff
    previous to and you’re just too magnificent. I actually like what you have obtained here, really like what you are
    stating and the way in which by which you are saying it.
    You make it enjoyable and you still care for to keep it sensible.
    I can not wait to learn far more from you. That is
    really a wonderful site. http://droga5.net/

    Reply

Leave a Comment