是否可以在 TypeScript 中声明和调用函数字典?
Is it possible to declare and call a dictionary of functions in TypeScript?
我正在做一些重构,想知道是否可以声明和初始化工厂函数字典,以枚举器为键,这样它就可以用作工厂函数的查找,然后可以叫什么?或者,或者,我是否在这方面走错了路并且错过了更优雅的解决方案。我按照 this answer 声明并初始化了一个类型化的字典,但我不确定我是否声明了签名正确,例如键是一个数字,值是一个函数。我已将我的代码简化为一个非常通用的示例 - 我知道它相当做作,但这样做的意图更加清晰。
// Types are enumerated as I have several different lists of types which I'd like to
// implement as an array of enumerators
enum ElementType {
TypeA,
TypeB,
TypeC
}
// Here, I'm trying to declare a dictionary where the key is a number and the value is a
// function
var ElementFactory: { [elementType: number]: () => {}; };
// Then I'm trying to declare these factory functions to return new objects
ElementFactory[ElementType.TypeA] = () => new ElementOfTypeA();
ElementFactory[ElementType.TypeB] = () => new ElementOfTypeB();
ElementFactory[ElementType.TypeC] = () => new ElementOfTypeC();
// And finally I'd like to be able to call this function like so such that they return
// instantiated objects as declared in the code block above
var a = ElementFactory[ElementType.TypeA]();
var b = ElementFactory[ElementType.TypeB]();
var c = ElementFactory[ElementType.TypeC]();
您的代码大部分是正确的,这种方法也行得通,但有一点可以改进:
// Here, I'm trying to declare a dictionary where the key is a number and the value is a
// function
var ElementFactory: { [elementType: number]: () => {}; };
在类型定义中,() => {}
表示 "a function that takes zero parameters and returns a {}
"。您可以在此处修改 return 类型以使其更具体,但遗憾的是,无论何时调用这些工厂函数,您仍然需要手动表达 returned 值的类型。例如,您可以这样做:
type AnyElementType = ElementOfTypeA | ElementOfTypeB | ElementOfTypeC;
var ElementFactory: { [elementType: number]: () => AnyElementType; };
...
// this type declaration will not work
var a: ElementOfTypeA = ElementFactory[ElementType.TypeA]();
// but these will
var b = <ElementOfTypeB>ElementFactory[ElementType.TypeB]();
var c = ElementFactory[ElementType.TypeC]() as ElementOfTypeC;
我正在做一些重构,想知道是否可以声明和初始化工厂函数字典,以枚举器为键,这样它就可以用作工厂函数的查找,然后可以叫什么?或者,或者,我是否在这方面走错了路并且错过了更优雅的解决方案。我按照 this answer 声明并初始化了一个类型化的字典,但我不确定我是否声明了签名正确,例如键是一个数字,值是一个函数。我已将我的代码简化为一个非常通用的示例 - 我知道它相当做作,但这样做的意图更加清晰。
// Types are enumerated as I have several different lists of types which I'd like to
// implement as an array of enumerators
enum ElementType {
TypeA,
TypeB,
TypeC
}
// Here, I'm trying to declare a dictionary where the key is a number and the value is a
// function
var ElementFactory: { [elementType: number]: () => {}; };
// Then I'm trying to declare these factory functions to return new objects
ElementFactory[ElementType.TypeA] = () => new ElementOfTypeA();
ElementFactory[ElementType.TypeB] = () => new ElementOfTypeB();
ElementFactory[ElementType.TypeC] = () => new ElementOfTypeC();
// And finally I'd like to be able to call this function like so such that they return
// instantiated objects as declared in the code block above
var a = ElementFactory[ElementType.TypeA]();
var b = ElementFactory[ElementType.TypeB]();
var c = ElementFactory[ElementType.TypeC]();
您的代码大部分是正确的,这种方法也行得通,但有一点可以改进:
// Here, I'm trying to declare a dictionary where the key is a number and the value is a // function var ElementFactory: { [elementType: number]: () => {}; };
在类型定义中,() => {}
表示 "a function that takes zero parameters and returns a {}
"。您可以在此处修改 return 类型以使其更具体,但遗憾的是,无论何时调用这些工厂函数,您仍然需要手动表达 returned 值的类型。例如,您可以这样做:
type AnyElementType = ElementOfTypeA | ElementOfTypeB | ElementOfTypeC;
var ElementFactory: { [elementType: number]: () => AnyElementType; };
...
// this type declaration will not work
var a: ElementOfTypeA = ElementFactory[ElementType.TypeA]();
// but these will
var b = <ElementOfTypeB>ElementFactory[ElementType.TypeB]();
var c = ElementFactory[ElementType.TypeC]() as ElementOfTypeC;