简体   繁体   English

如何在 TypeScript 中描述这个对象的形状?

[英]How to describe this object's shape in TypeScript?

The object looks like a mixture of dictionary and regular JavaScript object:该对象看起来像是字典和常规 JavaScript 对象的混合体:

var obj = {
  'dynamic prop1': { },
  'dynamic prop2': function () { },
  'dynamic prop3': function () { },
  'dynamic prop4': function () { },

  func: function() {},
  prop: null
};

The majority of the properties assigned to object will be represented by string-based ('dynamic prop') properties and will contain either object or function.分配给对象的大多数属性将由基于字符串(“动态道具”)的属性表示,并且将包含对象或函数。

Additionally it will contain two additional properties: prop and func.此外,它将包含两个附加属性:prop 和 func。

This is some text so it's not a code-only answer.这是一些文本,因此它不是纯代码答案。

interface MyObjType {
  [key: string]: (() => void) | { maybeYouHaveSomePropertiesToDeclare?: string };
  func(): void;
  prop: any;
}

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

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