我正在尝试访问 TypeScript 中对象的子对象。
所以object1是Object的子对象。当我控制台日志对象(父级)时,我清楚地看到属性“Children”。如果展开 Children,我会看到 Object1。
通常在 JavaScript 中,我会做类似的事情
var child = Object.children[0];
但是,当我在 TypeScript 中执行此操作时,它会给出语法错误
error TS2339: Property 'children' does not exist on type 'DisplayObject'.
在 TS 中是否有不同的方法?
编辑
示例代码:
this.game.world.children.forEach(function(child){
var constructorString: string = child.constructor.toString();
var className: string = constructorString.match(/\w+/g)[1];
if(className=='AcheivmentButton'){
for(var i=0; i<child.children.length; i++)
children.push(child.children[i]);
}
})
最佳答案
Is there a different way to do it in TS?
没有。这只是您正在使用的对象的定义存在差异。
最快的方法是简单地使用断言:
var child = (Object as any).children[0];
更多
关于javascript - 在 TypeScript 中访问对象的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032105/