繁体   English   中英

如何在typescript对象接口中键入字符串作为Object属性键?

[英]How to type a string as an Object property-key in a typescript Object interface?

我定义了一个接口 - 相关部分在这里:

      actors:{    
        name:{           // options (Object) is specific to each actor type
                        // see documentation at top of each actor
          factory:string,
          url:string,
          options:Object
        }
        //...
      }

我按如下方式实现接口:

      actors:{
        'unitcube':{ 
          factory:'Unitcube',
          url:'../src/app/models/stage/actors/objects/unitcube',
          options:{wireframe:false, 
                color:'red', 
                opacity:0.9, 
                transform:{t:[0.0,2.0,-3.0001],e:[0.0,1.0,0.0],s:[1.0,3.0,1.0]}
          } 
        }
        //...
      }//actors

接口传递tslint,但我收到tslint / tsc错误,接口类型和实现方式不同 - 特别是'unitcube'和name。

如何键入 - 指定字符串命名的对象列表? (即如何在界面的actor部分中定义'name'?)

谢谢。

interface IActors {
   actors: IActorType
}

interface IActorType {
  [name: string]: //so on and so forth
}

问题中定义的接口强制键实际上是“名称”。 当使用'unitcube'或任何其他actor的名称实现时,它被评估为不等于'name'。 [name:string]允许此键是动态的,因此可以使用任何类型为string的actor名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM