typescript - TypeScript 在 Map 中保留类
问题描述
解释我需要得到什么太复杂了,让我展示一个带有一些注释的代码。
我想它会清楚得多。
abstract class Base {
abstract getType(): string;
static test(): string[] {
return [];
}
}
class Foo extends Base {
foo = '123';
getType(): string {
return "foo";
}
static test(): string[] {
return ["a", "b", "c"]
}
}
class Bar extends Base {
bar = '321';
getType(): string {
return "bar";
}
static test(): string[] {
return ["a", "b", "c"]
}
}
// The following map is a temporary solution just to show what I want to get
// I supposed the map has to have another type instead of "any"
const m = new Map<string, any>();
m.set("foo", Foo);
m.set("bar", Bar);
console.log(m.get('foo').test()); // << I need to have access to static functions and it works with "any" type
function func(it: Base) {
const baseType = it.getType();
if (m.has(baseType)) {
const cl = m.get(baseType);
const item = it as cl; // << error: 'cl' refers to a value, but is being used as a type here. Did you mean 'typeof cl'?
console.log(item);
}
}
// I know I could do it by the following way, but in this case if I will want to add a few more new classes I will have to update many palces in the code.
// I'd like just to add a new class into the map without updating the entire code.
function func2(it: Foo|Bar) {
const baseType = it.getType();
console.log(baseType, it)
}
const fooInst = new Foo();
func(fooInst);
func2(fooInst);
我想得到什么:
- 我想要一个带有类列表的 Map 或 Object,以便我能够在循环中从它们中获取数据(来自静态函数)
- 我的代码中有一些地方函数将 Base 类作为参数。我需要将它转换为正确的类
getType()
。
解决方案
具有类列表的映射或对象
在 TypeScript 中,可以使用union来实现类列表。
为了使联合与类的静态成员一起工作,您还应该使用typeof
关键字。
转换为基于属性的类型
为了转换为基于基础对象属性的类型,您需要使用类型保护。
类型保护
类型保护是一种检查,确保给定的参数是类型。这可以是typeof
关键字,即typeof arg == "string"
,也可以是用户定义的类型保护;
function isArgString(arg: any): arg is string => {
return typeof arg == "string";
}
回顾
回顾一下,使您的地图工作的方法是使用联合,并且对于您的铸造,使用类型保护:
type myUnion = typeof Foo | typeof Bar;
export const isBar = (arg: Base): arg is Bar => {
return arg.getType() === 'bar';
}
export const isFoo = (arg: Base): arg is Foo => {
return arg.getType() === 'foo';
}
推荐阅读
- pyqt5 - ubuntu侧工具栏PyQt5中没有复制setwindowtitle
- python - 如何使用 AzureML SDK for Python 存储多个日志参数?
- here-api - 这里 javascript 供应商迁移
- typescript - 如何根据另一个属性的值定义属性的类型
- c# - 如何在内容页面中添加按钮以使用自定义渲染器 - 视图捕获图像
- r - 如何让 position_dodge 和 scale_x_date 一起工作?
- python - 引用 URL 的路径部分,但不引用参数
- reactjs - 嵌套 React Router 路径到 n 级
- c++ - C ++ - 将带有引用的长参数列表重构为结构
- java - Spring:如何自定义 UsernamePasswordAuthenticationFilter?