TS 获取变量类型



我想让变量类型给一个类参数。

certificate: CertificationInterface;
certificate = new CertificationFactory().create(param.type, {id: param.id});
helperService = new HelperFactoryService<certificate>().get();

第三行不起作用。

您可以尝试使用该接口。

new HelperFactoryService<CertificationInterface>().get();

TypeScript 类型在编译过程中被擦除(类除外(。您可以得到的是对象构造函数并将其用作类型。

在你的情况下param.constructor会给你一些函数,它是你的参数的构造函数(类(。

param instanceof param.constructor)将评估为true.

最新更新