这就是我想简单说明的事情:
interface A {
prop1: string;
}
interface B extends A {
prop2: string;
}
someImportantFunction(x: A): B {
var ret: B = x.prop2 = someCalculation(); //Error, but if possible x would now satisfy B
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我的问题的英文版是:如何在typescript中为现有对象添加新属性,目标是满足更多派生的接口定义?也许这是不可能的,或者我错过了一些明显的方法.
更新:另外,假设接口A上的属性列表很长,因此通过映射属性进行样板分配是费力且不干净的.
我也看到这样的东西会起作用,但它似乎是一个黑客:
someImportantFunction(x: A): B {
var ret: B = <B>x;
ret.prop2 = someCalculation();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Mathias
typescript ×1