简体   繁体   English

使用延迟初始化将符号定义为属性

[英]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.

相关问题 对 useState 使用延迟初始化有什么缺点吗? - Are there any disadvantages in using lazy initialization for useState? dyld:惰性符号绑定失败:->引用了我没有使用的包 - dyld: lazy symbol binding failed: -> references a package I am not using 有什么很酷的方法可以使用惰性评估序列来定义/计算PI? - Is there any cool method to define/calc PI using lazy evaluation sequence? jQuery UI自动完成的延迟初始化 - Lazy initialization of jquery UI autocomplete 惰性符号绑定失败:找不到符号:_FSEventStreamCreate - lazy symbol binding failed: Symbol not found: _FSEventStreamCreate 如何使用jsinterop定义数组属性? - How to define array property using jsinterop? 使用管道符号为嵌套对象属性设置默认值 - Set a default value for a nested object property using the pipe symbol React - 使用 Hooks 时,出现错误 - Object 不可迭代(无法读取属性 Symbol (Symbol.iterator)) - React - When using Hooks, I get an error - Object is not iterable (cannot read property Symbol (Symbol.iterator)) 使用 React Context API 时出错:“TypeError:Object 不可迭代(无法读取属性 Symbol(Symbol.iterator))” - Error while using React Context API: "TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))" dyld:惰性符号绑定失败,opencv - dyld: lazy symbol binding failed, opencv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM