我对 TypeScript 很陌生,我想知道是否存在重写代码的好方法,以避免以下代码中的 TSLint 错误“不允许通过字符串文字访问对象”
interface ECType
{
name: string;
type: string;
elementType?: string;
}
export var fields: { [structName: string]: Array<ECType>; } = { };
class ECStruct1 {
foo: string;
bar: number;
baz: boolean;
qux: number;
quux: number;
corge: ECStruct2[];
grault: ECStruct2;
constructor() {
...
}
}
fields['ECStruct1'] = [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'int' },
{ name: 'baz', type: 'bool' },
{ name: 'qux', type: 'long' },
{ name: 'quux', type: 'ulong' },
{ name: 'corge', type: 'array', elementType: 'ECStruct2' },
{ name: 'grault', type: 'ECStruct2' }
];
更新:最后,上面的内容将是一个超过 300ECStruct
秒的自生成文件的一部分,所以我想要类定义(例如ECStruct1
),然后是它的元描述(例如fields['ECStruct1']
)。