'Implement Func<T,TResult> delegate in Typescript
Fairly new to the node ecosystem and Typescript. I am looking for how to implement an abstract delegate Func<T,TResult> in Typescript.
A C# Example of what I want to do:
A ProxyClient for an API integration.
public interface IProxyClient
{
Task<TResult> GetData<TInput,TResult>(TInput input);
Task<TResult> AnotherOperation<TInput,TResult>(TInput input);
}
A "worker" class that will do stuff - including make the call to the API endpoint.
public abstract class Manager<TInput,TResult>
{
private readonly IProxyClient _proxyClient;
protected abstract Func<IProxyClient,TInput, Task<TResult>> proxyClientDelegate { get; }
public async Task<TResult> DoAThing(TInput input)
{
var result = await proxyClientDelegate.Invoke(_proxyClient, input);
//... Do Some other stuff
return result;
}
}
Derived classes of the manager would implement the actual method call:
public sealed class DerivedManager : Manager<InputClass, OutputClass>
{
protected override Func<IProxyClient, InputClass, Task<OutputClass>> proxyClientDelegate =>
(client, input) => client.GetData<InputClass, OutputClass>(input);
}
public class AnotherDerivedManager: Manager<SomeOtherInputClass,SomeOtherOutputClass>
{
protected override Func<IProxyClient, SomeOtherInputClass, Task<SomeOtherOutputClass>> proxyClientDelegate =>
(client, input) => client.AnotherOperation<SomeOtherInputClass, SomeOtherOutputClass>(input);
}
Allows for the abstraction in each derived manager.
public class Program
{
private readonly DerivedManager _derivedManager;
private readonly AnotherDerivedManager _anotherDerivedManager;
public Program(DerivedManager derivedManager, AnotherDerivedManager anotherDerivedManager)
{
_derivedManager = derivedManager;
_anotherDerivedManager = anotherDerivedManager;
}
public async Task Execute()
{
var inputClass = new InputClass();
var outputClass = await _derivedManager.DoAThing(inputClass);
var anotherInputClass = new SomeOtherInputClass();
var someOtherOutputClass = await _anotherDerivedManager.DoAThing(anotherInputClass);
}
}
How would I do this in typescript (is it even possible).
UPDATE: I found a solution. Will post the answer with examples shortly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
