1

I have this problem, and search for hours and can't find the right solution for it. I'm trying to create a generic function that revives an type and an object and convert the object to that specific type. but I'm having troubles to instantiate the generic type. I would like to know if there are something like in c# Activator.CreateInstance

handleResult<T>(response: Response):T {
    let jsonResp = response.json();
    let obj = jsonResp as Object;
    let resp: T = Object.CreateInstance(T); //        
    doTheMambo(resp, obj);
    return resp;
}
4
  • What does response.json() return? An object, or a string representation of that object? And if it returns an object directly, what are the differences between that object and T that you hope to resolve in your doTheMambo? Commented Dec 21, 2016 at 11:18
  • The response.json returns a json, an the doTheMambo will make some sort of parse between the two objects. Commented Dec 21, 2016 at 11:25
  • The two objects can have major differences between thems Commented Dec 21, 2016 at 11:28
  • 1
    There's no way to use T in runtime so you can't pass it. Can you please describe better what is the json you're using and what's the object you want to create? Commented Dec 21, 2016 at 11:47

1 Answer 1

2

In typescript, generic parameters are erased during the compilation and there is no trace of them in generated javascript code. So there's nothing like CreateInstance from c#.

But, again unlike c#, classes in typescript can be used at runtime just like any other objects, so if all you need is to create an instance of a class at runtime you can do that pretty easily (note that it applies only to classes, not to any type in general).

The syntax for generic class type argument looks a bit unusual: it's a literal type written as {new(...args: any[]): T}

Here is complete (it compiles) example that does something like you described in your question. For simplification, I supposed that all objects that need to be instantiated must conform to common interface Result (otherwise you have to do type cast to some known type in doTheMambo to do something useful).

interface Response { json(): any }

interface Result { kind: string }

function handleResult<T extends Result>(tClass: { new (...args: any[]): T }, response: Response): T {
    let jsonResp = response.json();
    let obj = jsonResp;
    let resp: T = new tClass();        
    doTheMambo(resp, obj);
    return resp;
}

class Result1 {
    kind = 'result1';
}

function doTheMambo<T extends Result>(response: T, obj: any) {
    console.log(response.kind);
}

const r: Result = handleResult(Result1, { json() { return null } });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.