[英]Define symbol as property using lazy initialization
I have this: 我有这个:
export const symbols = {
toString: Symbol('@xml.js.toString')
};
export class Node {
[key: string]: any;
[symbols.toString] = function(){
};
}
but I get this error: 但是我得到这个错误:
Which is: 这是:
A computed property name in class property declaration must refer to an expression whose type is a literal type or unique symbol type.
类属性声明中的计算属性名称必须引用其类型为文字类型或唯一符号类型的表达式。
Anyone know what's going on? 有人知道发生了什么吗?
If I put it in the constructor, I don't get an error: 如果将其放在构造函数中,不会出现错误:
As the error states, only unique symbols can be used as computed property names. 作为错误状态,只能将唯一符号用作计算的属性名称。
As described in the reference , 如参考资料中所述 ,
To enable treating symbols as unique literals a new type unique symbol is available.
为了能够将符号视为唯一文字,可以使用一种新型的唯一符号。 unique symbol is are subtype of symbol, and are produced only from calling Symbol() or Symbol.for(), or from explicit type annotations.
唯一符号是符号的子类型,并且仅通过调用Symbol()或Symbol.for()或显式类型注释生成。 The new type is only allowed on const declarations and readonly static properties, and in order to reference a specific unique symbol, you'll have to use the typeof operator.
仅在const声明和只读静态属性上才允许使用新类型,并且为了引用特定的唯一符号,您必须使用typeof运算符。 Each reference to a unique symbol implies a completely unique identity that's tied to a given declaration.
每个对唯一符号的引用都意味着与给定声明相关联的完全唯一的标识。
Due to listed limitations, object property cannot be unique. 由于列出的限制,对象属性不能唯一。
Instead, it can be: 相反,它可以是:
export namespace symbols {
export const toString = Symbol('@xml.js.toString');
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.