2
的说法,我想写,你解析类的类型(类,不是一个实例),那么该功能将实例基于该参数的实例的功能。描述函数参数以类为打字稿
这是最好的例子来解释:
//All possible paramter types must inherit from this base class
class Base { public name : string = ''; }
//These are possible classes that could be parsed to the function
class Foo extends Base { constructor() { super(); console.log("Foo instance created"); } }
class Bar extends Base { constructor() { super(); console.log("Bar instance created"); } }
//This function should take a class that inherits from 'Base' as a paramter - then it will create an instance
function Example(param : ?????????) : Base //I don't know what type the 'param' should be
{
return new param(); //Create instance?? How do I do this
}
//This should be the output - if it worked (but it doesn't)
Example(Foo); //Logs "Foo instance created""
Example(Bar); //Logs "Foo instance created""
//So if this worked, it would become possible to do this:
let b : Foo = Example(Foo);
let c : Bar = Example(Bar);
所以我的问题是:什么类型会为“样本”功能帕拉姆是什么?我将如何从函数内部创建一个param实例。
请注意,如果这个问题是重复的,我很抱歉 - 但我不知道这个过程的技术名称,所以很难研究。