使用惰性初始化将符号定义为 属性
Define symbol as property using lazy initialization
我有这个:
export const symbols = {
toString: Symbol('@xml.js.toString')
};
export class Node {
[key: string]: any;
[symbols.toString] = function(){
};
}
但是我得到这个错误:
即:
A computed property name in class property declaration must refer to
an expression whose type is a literal type or unique symbol type.
有人知道发生了什么事吗?
如果我把它放在构造函数中,我不会得到错误:
如错误所述,只有唯一符号可用作计算的 属性 名称。
所述
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. 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. Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.
由于列出的限制,对象 属性 不能是唯一的。
相反,它可以是:
export namespace symbols {
export const toString = Symbol('@xml.js.toString');
};
我有这个:
export const symbols = {
toString: Symbol('@xml.js.toString')
};
export class Node {
[key: string]: any;
[symbols.toString] = function(){
};
}
但是我得到这个错误:
即:
A computed property name in class property declaration must refer to an expression whose type is a literal type or unique symbol type.
有人知道发生了什么事吗?
如果我把它放在构造函数中,我不会得到错误:
如错误所述,只有唯一符号可用作计算的 属性 名称。
所述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. 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. Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.
由于列出的限制,对象 属性 不能是唯一的。
相反,它可以是:
export namespace symbols {
export const toString = Symbol('@xml.js.toString');
};