[英]Using ES6 Symbols with typescript
I am trying to run this simple line of code — 我正在尝试运行这个简单的代码行 -
let FUNJECTOR_KEY = Symbol.for('funjector')
But I keep getting the error — Cannot find name 'Symbol'.
但我一直收到错误 - Cannot find name 'Symbol'.
I am new to typescript, so I am not sure if I need to include something? 我是打字稿的新手,所以我不确定是否需要包含一些内容?
In my case I don't want to use a poly fill as explained here — Using es-6 symbols in typescript 在我的情况下,我不想使用这里解释的poly填充 - 在typescript中使用es-6符号
TypeScript compiler transpiles TS into JS. TypeScript编译器将TS转换为JS。 TSC cannot find the declaration for Symbol
in es5 mode. TSC在es5模式下找不到Symbol
的声明。 So your error is purely in compile-time. 所以你的错误纯粹是在编译时。 You don't need polyfill for runtime. 您不需要polyfill用于运行时。
To address this, you can either change your compiling target to es6
, so that Symbol
is defined in standard library. 要解决此问题,您可以将编译目标更改为es6
,以便在标准库中定义Symbol
。 Or you can manually add the definition ( source ). 或者您可以手动添加定义( 源 )。
declare class Symbol {
/** Returns a string representation of an object. */
toString(): string;
/** Returns the primitive value of the specified object. */
valueOf(): Object;
/**
* Returns a new unique Symbol value.
* @param description Description of the new Symbol object.
*/
constructor(description?: string);
/**
* Returns a Symbol object from the global symbol registry matching the given key if found.
* Otherwise, returns a new symbol with this key.
* @param key key to search for.
*/
static for(key: string): Symbol;
/**
* Returns a key from the global symbol registry matching the given Symbol if found.
* Otherwise, returns a undefined.
* @param sym Symbol to find the key for.
*/
static keyFor(sym: Symbol): string;
}
// Well-known Symbols
declare module Symbol {
/**
* A method that determines if a constructor object recognizes an object as one of the
* constructor’s instances.Called by the semantics of the instanceof operator.
*/
const hasInstance: Symbol;
/**
* A Boolean value that if true indicates that an object should be flatten to its array
* elements by Array.prototype.concat.
*/
const isConcatSpreadable: Symbol;
/**
* A Boolean value that if true indicates that an object may be used as a regular expression.
*/
const isRegExp: Symbol;
/**
* A method that returns the default iterator for an object.Called by the semantics of the
* for-of statement.
*/
const iterator: Symbol;
/**
* A method that converts an object to a corresponding primitive value.Called by the
* ToPrimitive abstract operation.
*/
const toPrimitive: Symbol;
/**
* A String value that is used in the creation of the default string description of an object.
* Called by the built- in method Object.prototype.toString.
*/
const toStringTag: Symbol;
/**
* An Object whose own property names are property names that are excluded from the with
* environment bindings of the associated objects.
*/
const unscopables: Symbol;
}
Caveat: Some type checking does not work on symbol
. 警告:某些类型检查不适用于symbol
。 For example, you cannot declare a property computed to symbol on interface. 例如,您不能在接口上声明计算为符号的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.