最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using ES6 Symbols with typescript - Stack Overflow

programmeradmin4浏览0评论

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'. 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

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'. 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

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Mar 27, 2016 at 7:06 tusharmathtusharmath 11k13 gold badges61 silver badges85 bronze badges 4
  • Possible duplicate of Using es-6 symbols in typescript – Fidan Hakaj Commented Mar 27, 2016 at 7:15
  • I don't want to use a polyfill. – tusharmath Commented Mar 27, 2016 at 7:23
  • Are you piling to ES5 or to ES6 as the target? – jfriend00 Commented Mar 27, 2016 at 7:26
  • I see! By default it was piling to ES5, I changed the target to ES6 and it worked! – tusharmath Commented Mar 27, 2016 at 7:29
Add a ment  | 

1 Answer 1

Reset to default 6

TypeScript piler transpiles TS into JS. TSC cannot find the declaration for Symbol in es5 mode. So your error is purely in pile-time. You don't need polyfill for runtime.

To address this, you can either change your piling target to es6, so that Symbol is defined in standard library. 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. For example, you cannot declare a property puted to symbol on interface.

发布评论

评论列表(0)

  1. 暂无评论