[英]How can I use a class type as a map key in TypeScript?
I would like to store some informations in a TypeScript (JavaScript) map under a key which reflects the type of a class. 我想将一些信息存储在TypeScript(JavaScript)映射中的某个键下,该键反映了类的类型。 This is because the stored data is actually static and applies per type, not per instance.
这是因为存储的数据实际上是静态的,并且按类型而不是按实例应用。
Here's how I have declared the Map atm: 这是我声明Map atm的方法:
private static followSetsByATN: Map<number, FollowSetsPerState> = new Map();
However, number
should be a class type instead. 但是,
number
应改为类类型。 How can this be achieved? 如何做到这一点?
If you have an object ( {}
) as a map then the keys must be strings (or numbers which are converted to strings automatically). 如果您有一个对象(
{}
)作为映射,则键必须是字符串(或数字会自动转换为字符串)。
In this case you can use the toString()
method: 在这种情况下,可以使用
toString()
方法:
class A { }
console.log(A.toString());
Will print: 将打印:
function A() {
}
You can also use the name
property: 您还可以使用
name
属性:
console.log(A.name); // "A"
You can also override the toString()
method to return something of your own: 您还可以重写
toString()
方法以返回自己的内容:
class A {
static toString() {
return "class A";
}
}
console.log(A.toString()); // "class A"
And then: 然后:
let m = {} as { [name: string]: string };
m[A.toString()] = "something";
// or
m[A.name] = "something";
If you are using a Map
then all of the above still work, but you are not restricted to have string keys therefor you can use the class itself: 如果您使用的是
Map
则上述所有方法仍然有效,但不限于具有字符串键,您可以使用类本身:
let m = new Map<{ new (): A }, string>();
m.set(A, A.toString());
console.log(m.get(A)); // "class A"
If you have an instance of a class, you can get the class using the constructor
property: 如果有一个类的实例,则可以使用
constructor
属性获取该类:
let a = new A();
...
m.set(a.constructor, SOME_VALUE);
The constructor
property comes from object
and it looks like so: constructor
属性来自object
,它看起来像这样:
interface Object {
/** The initial value of Object.prototype.constructor is the standard built-in Object constructor. */
constructor: Function;
...
}
So you always get a Function
and you'll need to cast: 因此,您始终可以获得一个
Function
并且需要强制转换:
m.set(a.constructor as typeof A, SOME_VALUE);
As you probably don't know the type of the class, you'll need to do something else. 由于您可能不知道类的类型,因此您需要做其他事情。
You can cast to any
, but that's not very pretty. 您可以投射到
any
,但这不是很漂亮。
Here's a working solution that should work well for you: 这是一个适合您的工作解决方案:
interface Base { }
type BaseContructor = { new (): Base };
class A implements Base { }
class B implements Base { }
let m = new Map<BaseContructor, string>();
let a = new A();
let b = new B();
m.set(a.constructor as BaseContructor, "value");
m.set(b.constructor as BaseContructor, "value");
( code in playground ) ( 操场上的代码 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.