基本上,我想做的是从静态类获取值。像这样:
export enum Test {
A = 10,
B = 20,
C = 30
}
export class TestObject
{
constructor(public ValueA: string, public ValueB: Date){}
}
export class TestValues {
[key: number] : TestObject;
public static 10: TestObject = new TestObject ('AAA', new Date());
public static 20: TestObject = new TestObject ('BBB', new Date());
public static 30: TestObject = new TestObject ('CCC', new Date());
}
var a = Test.A as number;
var result = TestValues[a];
这将返回错误:
Element implicitly has an 'any' type because
type 'typeof TestValues' has no index signature.
请参见打字稿游乐场HERE
答案 0 :(得分:0)
好的,所以我不再通过输出静态类来解决它。我将其设置为普通类,并将其实例化并导出为常量。
答案:
export enum Test {
A = 10,
B = 20,
C = 30
}
export class TestObject
{
constructor(public ValueA: string, public ValueB: Date){}
}
class TestValuesDefinition {
[key: number] : TestObject;
public 10: TestObject = new TestObject ('AAA', new Date());
public 20: TestObject = new TestObject ('BBB', new Date());
public 30: TestObject = new TestObject ('CCC', new Date());
}
export const TestValues = new TestValuesDefinition();
// And than in the other file
var a = Test.A;
var result = TestValues[a];