1
votes

For a project, I would to separate Implementation of a function and the call of this function.

It's something like this:

function identity<T, R>(arg: T): R {
    return { result: arg };
}

const test = identity<string, { result: string }>;

console.log(test('test'))

I got this error: "test is not a function"

Is there a way to perform type hinting like this?

Thanks

1

1 Answers

5
votes

Here, this does what I think you are trying to do:

type R<U> = {
    result: U
};

function identity<T>(arg: T): R<T> {
    return { result: arg };
}

const test : (a: string) => R<string> = identity;

Here is another way to think of the same thing:

type R<U> = {
    result: U
};

type RFunc<T> = (a: T) => R<T>;

const identity = function<T>(arg: T): R<T> {
    return { result: arg };
}

const test : RFunc<string> = identity;