【问题标题】:Using ES6 Symbols with typescript使用带有打字稿的 ES6 符号
【发布时间】:2016-03-27 07:06:24
【问题描述】:

我正在尝试运行这行简单的代码——

let FUNJECTOR_KEY = Symbol.for('funjector')

但我不断收到错误消息 — Cannot find name 'Symbol'. 我是打字稿的新手,所以我不确定是否需要包含一些内容?

就我而言,我不想使用此处解释的多边形填充 — Using es-6 symbols in typescript

【问题讨论】:

  • 我不想使用 polyfill。
  • 你是编译到ES5还是以ES6为目标?
  • 我明白了!默认情况下它编译为 ES5,我将目标更改为 ES6 并且它可以工作!

标签: javascript typescript ecmascript-6


【解决方案1】:

TypeScript 编译器将 TS 转换为 JS。 TSC 在 es5 模式下找不到 Symbol 的声明。所以你的错误纯粹是在编译时。运行时不需要 polyfill。

要解决这个问题,您可以将编译目标更改为es6,以便在标准库中定义Symbol。或者您可以手动添加定义 (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;
}

警告: 某些类型检查不适用于symbol。例如,您不能在接口上声明计算为符号的属性。

【讨论】:

猜你喜欢
  • 2021-04-26
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2016-12-12
  • 2021-01-14
  • 2015-10-23
相关资源
最近更新 更多