仅仅通过运行时接口才能创建对象实例吗?像在运行时通过接口创建实例
interface IModel {
name: string;
}
function createInstance() {
return something to make instance
}
console.log(createInstance()); // out: {name:null}
仅仅通过运行时接口才能创建对象实例吗?像在运行时通过接口创建实例
interface IModel {
name: string;
}
function createInstance() {
return something to make instance
}
console.log(createInstance()); // out: {name:null}
其不可能的,因为接口IST编译js的
东西:
interface IModel {
name: string | null;
}
function createInstance(name: string | null): IModel {
return { name };
}
console.log(createInstance(null));
不,我想建立对象的实例。我只有接口没有specyfic内容,并且我想将此接口作为对象和构建对象的定义。它看起来像使用Object.assign重写对象到对象,但接口没有得到一个实例,直到我没有实现一些类上的thiem并创建这个类对象,所以我的问题是“这可能” –
在javascript ** everything **是对象,即使是'{}'也是一个对象。所以我的答案中的代码确实创建了一个对象。 'Object.assign'将会返回完全相同的结果。我不确定你在追求什么。 –
是的,但在你的代码我必须知道编译前的字段名称,我试图通过任何接口生成对象,没有硬编码 –
的接口无法构造。 –