使用typescript时,声明的接口可能如下所示:
interface MyInterface {
test: string;
}
具有额外属性的实现可以是这样的:
class MyTest implements MyInterface {
test: string;
newTest: string;
}
Example(此处变量"reduced"仍包含属性"newTest"):
var test: MyTest = {test: "hello", newTest: "world"}
var reduced: MyInterface = test; // something clever is needed
Question
一般来说,如何使"reduced"变量只包含在"MyInterface"接口中声明的属性.
Why
当try 将"简化"变量与Angular 变量一起使用时,会出现问题.toJson在发送给rest服务之前——toJson方法会转换newTest变量,即使在编译期间实例上无法访问该变量,这会使rest服务不接受json,因为它具有不应该存在的属性.